]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/metaslab.c
Illumos #3598
[mirror_zfs.git] / module / zfs / metaslab.c
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
24 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25 */
26
27 #include <sys/zfs_context.h>
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>
34
35 #define WITH_DF_BLOCK_ALLOCATOR
36
37 /*
38 * Allow allocations to switch to gang blocks quickly. We do this to
39 * avoid having to load lots of space_maps in a given txg. There are,
40 * however, some cases where we want to avoid "fast" ganging and instead
41 * we want to do an exhaustive search of all metaslabs on this device.
42 * Currently we don't allow any gang, zil, or dump device related allocations
43 * to "fast" gang.
44 */
45 #define CAN_FASTGANG(flags) \
46 (!((flags) & (METASLAB_GANG_CHILD | METASLAB_GANG_HEADER | \
47 METASLAB_GANG_AVOID)))
48
49 uint64_t metaslab_aliquot = 512ULL << 10;
50 uint64_t metaslab_gang_bang = SPA_MAXBLOCKSIZE + 1; /* force gang blocks */
51
52 /*
53 * The in-core space map representation is more compact than its on-disk form.
54 * The zfs_condense_pct determines how much more compact the in-core
55 * space_map representation must be before we compact it on-disk.
56 * Values should be greater than or equal to 100.
57 */
58 int zfs_condense_pct = 200;
59
60 /*
61 * This value defines the number of allowed allocation failures per vdev.
62 * If a device reaches this threshold in a given txg then we consider skipping
63 * allocations on that device.
64 */
65 int zfs_mg_alloc_failures;
66
67 /*
68 * Metaslab debugging: when set, keeps all space maps in core to verify frees.
69 */
70 int metaslab_debug = 0;
71
72 /*
73 * Minimum size which forces the dynamic allocator to change
74 * it's allocation strategy. Once the space map cannot satisfy
75 * an allocation of this size then it switches to using more
76 * aggressive strategy (i.e search by size rather than offset).
77 */
78 uint64_t metaslab_df_alloc_threshold = SPA_MAXBLOCKSIZE;
79
80 /*
81 * The minimum free space, in percent, which must be available
82 * in a space map to continue allocations in a first-fit fashion.
83 * Once the space_map's free space drops below this level we dynamically
84 * switch to using best-fit allocations.
85 */
86 int metaslab_df_free_pct = 4;
87
88 /*
89 * A metaslab is considered "free" if it contains a contiguous
90 * segment which is greater than metaslab_min_alloc_size.
91 */
92 uint64_t metaslab_min_alloc_size = DMU_MAX_ACCESS;
93
94 /*
95 * Max number of space_maps to prefetch.
96 */
97 int metaslab_prefetch_limit = SPA_DVAS_PER_BP;
98
99 /*
100 * Percentage bonus multiplier for metaslabs that are in the bonus area.
101 */
102 int metaslab_smo_bonus_pct = 150;
103
104 /*
105 * ==========================================================================
106 * Metaslab classes
107 * ==========================================================================
108 */
109 metaslab_class_t *
110 metaslab_class_create(spa_t *spa, space_map_ops_t *ops)
111 {
112 metaslab_class_t *mc;
113
114 mc = kmem_zalloc(sizeof (metaslab_class_t), KM_PUSHPAGE);
115
116 mc->mc_spa = spa;
117 mc->mc_rotor = NULL;
118 mc->mc_ops = ops;
119 mutex_init(&mc->mc_fastwrite_lock, NULL, MUTEX_DEFAULT, NULL);
120
121 return (mc);
122 }
123
124 void
125 metaslab_class_destroy(metaslab_class_t *mc)
126 {
127 ASSERT(mc->mc_rotor == NULL);
128 ASSERT(mc->mc_alloc == 0);
129 ASSERT(mc->mc_deferred == 0);
130 ASSERT(mc->mc_space == 0);
131 ASSERT(mc->mc_dspace == 0);
132
133 mutex_destroy(&mc->mc_fastwrite_lock);
134 kmem_free(mc, sizeof (metaslab_class_t));
135 }
136
137 int
138 metaslab_class_validate(metaslab_class_t *mc)
139 {
140 metaslab_group_t *mg;
141 vdev_t *vd;
142
143 /*
144 * Must hold one of the spa_config locks.
145 */
146 ASSERT(spa_config_held(mc->mc_spa, SCL_ALL, RW_READER) ||
147 spa_config_held(mc->mc_spa, SCL_ALL, RW_WRITER));
148
149 if ((mg = mc->mc_rotor) == NULL)
150 return (0);
151
152 do {
153 vd = mg->mg_vd;
154 ASSERT(vd->vdev_mg != NULL);
155 ASSERT3P(vd->vdev_top, ==, vd);
156 ASSERT3P(mg->mg_class, ==, mc);
157 ASSERT3P(vd->vdev_ops, !=, &vdev_hole_ops);
158 } while ((mg = mg->mg_next) != mc->mc_rotor);
159
160 return (0);
161 }
162
163 void
164 metaslab_class_space_update(metaslab_class_t *mc, int64_t alloc_delta,
165 int64_t defer_delta, int64_t space_delta, int64_t dspace_delta)
166 {
167 atomic_add_64(&mc->mc_alloc, alloc_delta);
168 atomic_add_64(&mc->mc_deferred, defer_delta);
169 atomic_add_64(&mc->mc_space, space_delta);
170 atomic_add_64(&mc->mc_dspace, dspace_delta);
171 }
172
173 uint64_t
174 metaslab_class_get_alloc(metaslab_class_t *mc)
175 {
176 return (mc->mc_alloc);
177 }
178
179 uint64_t
180 metaslab_class_get_deferred(metaslab_class_t *mc)
181 {
182 return (mc->mc_deferred);
183 }
184
185 uint64_t
186 metaslab_class_get_space(metaslab_class_t *mc)
187 {
188 return (mc->mc_space);
189 }
190
191 uint64_t
192 metaslab_class_get_dspace(metaslab_class_t *mc)
193 {
194 return (spa_deflate(mc->mc_spa) ? mc->mc_dspace : mc->mc_space);
195 }
196
197 /*
198 * ==========================================================================
199 * Metaslab groups
200 * ==========================================================================
201 */
202 static int
203 metaslab_compare(const void *x1, const void *x2)
204 {
205 const metaslab_t *m1 = x1;
206 const metaslab_t *m2 = x2;
207
208 if (m1->ms_weight < m2->ms_weight)
209 return (1);
210 if (m1->ms_weight > m2->ms_weight)
211 return (-1);
212
213 /*
214 * If the weights are identical, use the offset to force uniqueness.
215 */
216 if (m1->ms_map->sm_start < m2->ms_map->sm_start)
217 return (-1);
218 if (m1->ms_map->sm_start > m2->ms_map->sm_start)
219 return (1);
220
221 ASSERT3P(m1, ==, m2);
222
223 return (0);
224 }
225
226 metaslab_group_t *
227 metaslab_group_create(metaslab_class_t *mc, vdev_t *vd)
228 {
229 metaslab_group_t *mg;
230
231 mg = kmem_zalloc(sizeof (metaslab_group_t), KM_PUSHPAGE);
232 mutex_init(&mg->mg_lock, NULL, MUTEX_DEFAULT, NULL);
233 avl_create(&mg->mg_metaslab_tree, metaslab_compare,
234 sizeof (metaslab_t), offsetof(struct metaslab, ms_group_node));
235 mg->mg_vd = vd;
236 mg->mg_class = mc;
237 mg->mg_activation_count = 0;
238
239 return (mg);
240 }
241
242 void
243 metaslab_group_destroy(metaslab_group_t *mg)
244 {
245 ASSERT(mg->mg_prev == NULL);
246 ASSERT(mg->mg_next == NULL);
247 /*
248 * We may have gone below zero with the activation count
249 * either because we never activated in the first place or
250 * because we're done, and possibly removing the vdev.
251 */
252 ASSERT(mg->mg_activation_count <= 0);
253
254 avl_destroy(&mg->mg_metaslab_tree);
255 mutex_destroy(&mg->mg_lock);
256 kmem_free(mg, sizeof (metaslab_group_t));
257 }
258
259 void
260 metaslab_group_activate(metaslab_group_t *mg)
261 {
262 metaslab_class_t *mc = mg->mg_class;
263 metaslab_group_t *mgprev, *mgnext;
264
265 ASSERT(spa_config_held(mc->mc_spa, SCL_ALLOC, RW_WRITER));
266
267 ASSERT(mc->mc_rotor != mg);
268 ASSERT(mg->mg_prev == NULL);
269 ASSERT(mg->mg_next == NULL);
270 ASSERT(mg->mg_activation_count <= 0);
271
272 if (++mg->mg_activation_count <= 0)
273 return;
274
275 mg->mg_aliquot = metaslab_aliquot * MAX(1, mg->mg_vd->vdev_children);
276
277 if ((mgprev = mc->mc_rotor) == NULL) {
278 mg->mg_prev = mg;
279 mg->mg_next = mg;
280 } else {
281 mgnext = mgprev->mg_next;
282 mg->mg_prev = mgprev;
283 mg->mg_next = mgnext;
284 mgprev->mg_next = mg;
285 mgnext->mg_prev = mg;
286 }
287 mc->mc_rotor = mg;
288 }
289
290 void
291 metaslab_group_passivate(metaslab_group_t *mg)
292 {
293 metaslab_class_t *mc = mg->mg_class;
294 metaslab_group_t *mgprev, *mgnext;
295
296 ASSERT(spa_config_held(mc->mc_spa, SCL_ALLOC, RW_WRITER));
297
298 if (--mg->mg_activation_count != 0) {
299 ASSERT(mc->mc_rotor != mg);
300 ASSERT(mg->mg_prev == NULL);
301 ASSERT(mg->mg_next == NULL);
302 ASSERT(mg->mg_activation_count < 0);
303 return;
304 }
305
306 mgprev = mg->mg_prev;
307 mgnext = mg->mg_next;
308
309 if (mg == mgnext) {
310 mc->mc_rotor = NULL;
311 } else {
312 mc->mc_rotor = mgnext;
313 mgprev->mg_next = mgnext;
314 mgnext->mg_prev = mgprev;
315 }
316
317 mg->mg_prev = NULL;
318 mg->mg_next = NULL;
319 }
320
321 static void
322 metaslab_group_add(metaslab_group_t *mg, metaslab_t *msp)
323 {
324 mutex_enter(&mg->mg_lock);
325 ASSERT(msp->ms_group == NULL);
326 msp->ms_group = mg;
327 msp->ms_weight = 0;
328 avl_add(&mg->mg_metaslab_tree, msp);
329 mutex_exit(&mg->mg_lock);
330 }
331
332 static void
333 metaslab_group_remove(metaslab_group_t *mg, metaslab_t *msp)
334 {
335 mutex_enter(&mg->mg_lock);
336 ASSERT(msp->ms_group == mg);
337 avl_remove(&mg->mg_metaslab_tree, msp);
338 msp->ms_group = NULL;
339 mutex_exit(&mg->mg_lock);
340 }
341
342 static void
343 metaslab_group_sort(metaslab_group_t *mg, metaslab_t *msp, uint64_t weight)
344 {
345 /*
346 * Although in principle the weight can be any value, in
347 * practice we do not use values in the range [1, 510].
348 */
349 ASSERT(weight >= SPA_MINBLOCKSIZE-1 || weight == 0);
350 ASSERT(MUTEX_HELD(&msp->ms_lock));
351
352 mutex_enter(&mg->mg_lock);
353 ASSERT(msp->ms_group == mg);
354 avl_remove(&mg->mg_metaslab_tree, msp);
355 msp->ms_weight = weight;
356 avl_add(&mg->mg_metaslab_tree, msp);
357 mutex_exit(&mg->mg_lock);
358 }
359
360 /*
361 * ==========================================================================
362 * Common allocator routines
363 * ==========================================================================
364 */
365 static int
366 metaslab_segsize_compare(const void *x1, const void *x2)
367 {
368 const space_seg_t *s1 = x1;
369 const space_seg_t *s2 = x2;
370 uint64_t ss_size1 = s1->ss_end - s1->ss_start;
371 uint64_t ss_size2 = s2->ss_end - s2->ss_start;
372
373 if (ss_size1 < ss_size2)
374 return (-1);
375 if (ss_size1 > ss_size2)
376 return (1);
377
378 if (s1->ss_start < s2->ss_start)
379 return (-1);
380 if (s1->ss_start > s2->ss_start)
381 return (1);
382
383 return (0);
384 }
385
386 #if defined(WITH_FF_BLOCK_ALLOCATOR) || \
387 defined(WITH_DF_BLOCK_ALLOCATOR) || \
388 defined(WITH_CDF_BLOCK_ALLOCATOR)
389 /*
390 * This is a helper function that can be used by the allocator to find
391 * a suitable block to allocate. This will search the specified AVL
392 * tree looking for a block that matches the specified criteria.
393 */
394 static uint64_t
395 metaslab_block_picker(avl_tree_t *t, uint64_t *cursor, uint64_t size,
396 uint64_t align)
397 {
398 space_seg_t *ss, ssearch;
399 avl_index_t where;
400
401 ssearch.ss_start = *cursor;
402 ssearch.ss_end = *cursor + size;
403
404 ss = avl_find(t, &ssearch, &where);
405 if (ss == NULL)
406 ss = avl_nearest(t, where, AVL_AFTER);
407
408 while (ss != NULL) {
409 uint64_t offset = P2ROUNDUP(ss->ss_start, align);
410
411 if (offset + size <= ss->ss_end) {
412 *cursor = offset + size;
413 return (offset);
414 }
415 ss = AVL_NEXT(t, ss);
416 }
417
418 /*
419 * If we know we've searched the whole map (*cursor == 0), give up.
420 * Otherwise, reset the cursor to the beginning and try again.
421 */
422 if (*cursor == 0)
423 return (-1ULL);
424
425 *cursor = 0;
426 return (metaslab_block_picker(t, cursor, size, align));
427 }
428 #endif /* WITH_FF/DF/CDF_BLOCK_ALLOCATOR */
429
430 static void
431 metaslab_pp_load(space_map_t *sm)
432 {
433 space_seg_t *ss;
434
435 ASSERT(sm->sm_ppd == NULL);
436 sm->sm_ppd = kmem_zalloc(64 * sizeof (uint64_t), KM_PUSHPAGE);
437
438 sm->sm_pp_root = kmem_alloc(sizeof (avl_tree_t), KM_PUSHPAGE);
439 avl_create(sm->sm_pp_root, metaslab_segsize_compare,
440 sizeof (space_seg_t), offsetof(struct space_seg, ss_pp_node));
441
442 for (ss = avl_first(&sm->sm_root); ss; ss = AVL_NEXT(&sm->sm_root, ss))
443 avl_add(sm->sm_pp_root, ss);
444 }
445
446 static void
447 metaslab_pp_unload(space_map_t *sm)
448 {
449 void *cookie = NULL;
450
451 kmem_free(sm->sm_ppd, 64 * sizeof (uint64_t));
452 sm->sm_ppd = NULL;
453
454 while (avl_destroy_nodes(sm->sm_pp_root, &cookie) != NULL) {
455 /* tear down the tree */
456 }
457
458 avl_destroy(sm->sm_pp_root);
459 kmem_free(sm->sm_pp_root, sizeof (avl_tree_t));
460 sm->sm_pp_root = NULL;
461 }
462
463 /* ARGSUSED */
464 static void
465 metaslab_pp_claim(space_map_t *sm, uint64_t start, uint64_t size)
466 {
467 /* No need to update cursor */
468 }
469
470 /* ARGSUSED */
471 static void
472 metaslab_pp_free(space_map_t *sm, uint64_t start, uint64_t size)
473 {
474 /* No need to update cursor */
475 }
476
477 /*
478 * Return the maximum contiguous segment within the metaslab.
479 */
480 uint64_t
481 metaslab_pp_maxsize(space_map_t *sm)
482 {
483 avl_tree_t *t = sm->sm_pp_root;
484 space_seg_t *ss;
485
486 if (t == NULL || (ss = avl_last(t)) == NULL)
487 return (0ULL);
488
489 return (ss->ss_end - ss->ss_start);
490 }
491
492 #if defined(WITH_FF_BLOCK_ALLOCATOR)
493 /*
494 * ==========================================================================
495 * The first-fit block allocator
496 * ==========================================================================
497 */
498 static uint64_t
499 metaslab_ff_alloc(space_map_t *sm, uint64_t size)
500 {
501 avl_tree_t *t = &sm->sm_root;
502 uint64_t align = size & -size;
503 uint64_t *cursor = (uint64_t *)sm->sm_ppd + highbit(align) - 1;
504
505 return (metaslab_block_picker(t, cursor, size, align));
506 }
507
508 /* ARGSUSED */
509 boolean_t
510 metaslab_ff_fragmented(space_map_t *sm)
511 {
512 return (B_TRUE);
513 }
514
515 static space_map_ops_t metaslab_ff_ops = {
516 metaslab_pp_load,
517 metaslab_pp_unload,
518 metaslab_ff_alloc,
519 metaslab_pp_claim,
520 metaslab_pp_free,
521 metaslab_pp_maxsize,
522 metaslab_ff_fragmented
523 };
524
525 space_map_ops_t *zfs_metaslab_ops = &metaslab_ff_ops;
526 #endif /* WITH_FF_BLOCK_ALLOCATOR */
527
528 #if defined(WITH_DF_BLOCK_ALLOCATOR)
529 /*
530 * ==========================================================================
531 * Dynamic block allocator -
532 * Uses the first fit allocation scheme until space get low and then
533 * adjusts to a best fit allocation method. Uses metaslab_df_alloc_threshold
534 * and metaslab_df_free_pct to determine when to switch the allocation scheme.
535 * ==========================================================================
536 */
537 static uint64_t
538 metaslab_df_alloc(space_map_t *sm, uint64_t size)
539 {
540 avl_tree_t *t = &sm->sm_root;
541 uint64_t align = size & -size;
542 uint64_t *cursor = (uint64_t *)sm->sm_ppd + highbit(align) - 1;
543 uint64_t max_size = metaslab_pp_maxsize(sm);
544 int free_pct = sm->sm_space * 100 / sm->sm_size;
545
546 ASSERT(MUTEX_HELD(sm->sm_lock));
547 ASSERT3U(avl_numnodes(&sm->sm_root), ==, avl_numnodes(sm->sm_pp_root));
548
549 if (max_size < size)
550 return (-1ULL);
551
552 /*
553 * If we're running low on space switch to using the size
554 * sorted AVL tree (best-fit).
555 */
556 if (max_size < metaslab_df_alloc_threshold ||
557 free_pct < metaslab_df_free_pct) {
558 t = sm->sm_pp_root;
559 *cursor = 0;
560 }
561
562 return (metaslab_block_picker(t, cursor, size, 1ULL));
563 }
564
565 static boolean_t
566 metaslab_df_fragmented(space_map_t *sm)
567 {
568 uint64_t max_size = metaslab_pp_maxsize(sm);
569 int free_pct = sm->sm_space * 100 / sm->sm_size;
570
571 if (max_size >= metaslab_df_alloc_threshold &&
572 free_pct >= metaslab_df_free_pct)
573 return (B_FALSE);
574
575 return (B_TRUE);
576 }
577
578 static space_map_ops_t metaslab_df_ops = {
579 metaslab_pp_load,
580 metaslab_pp_unload,
581 metaslab_df_alloc,
582 metaslab_pp_claim,
583 metaslab_pp_free,
584 metaslab_pp_maxsize,
585 metaslab_df_fragmented
586 };
587
588 space_map_ops_t *zfs_metaslab_ops = &metaslab_df_ops;
589 #endif /* WITH_DF_BLOCK_ALLOCATOR */
590
591 /*
592 * ==========================================================================
593 * Other experimental allocators
594 * ==========================================================================
595 */
596 #if defined(WITH_CDF_BLOCK_ALLOCATOR)
597 static uint64_t
598 metaslab_cdf_alloc(space_map_t *sm, uint64_t size)
599 {
600 avl_tree_t *t = &sm->sm_root;
601 uint64_t *cursor = (uint64_t *)sm->sm_ppd;
602 uint64_t *extent_end = (uint64_t *)sm->sm_ppd + 1;
603 uint64_t max_size = metaslab_pp_maxsize(sm);
604 uint64_t rsize = size;
605 uint64_t offset = 0;
606
607 ASSERT(MUTEX_HELD(sm->sm_lock));
608 ASSERT3U(avl_numnodes(&sm->sm_root), ==, avl_numnodes(sm->sm_pp_root));
609
610 if (max_size < size)
611 return (-1ULL);
612
613 ASSERT3U(*extent_end, >=, *cursor);
614
615 /*
616 * If we're running low on space switch to using the size
617 * sorted AVL tree (best-fit).
618 */
619 if ((*cursor + size) > *extent_end) {
620
621 t = sm->sm_pp_root;
622 *cursor = *extent_end = 0;
623
624 if (max_size > 2 * SPA_MAXBLOCKSIZE)
625 rsize = MIN(metaslab_min_alloc_size, max_size);
626 offset = metaslab_block_picker(t, extent_end, rsize, 1ULL);
627 if (offset != -1)
628 *cursor = offset + size;
629 } else {
630 offset = metaslab_block_picker(t, cursor, rsize, 1ULL);
631 }
632 ASSERT3U(*cursor, <=, *extent_end);
633 return (offset);
634 }
635
636 static boolean_t
637 metaslab_cdf_fragmented(space_map_t *sm)
638 {
639 uint64_t max_size = metaslab_pp_maxsize(sm);
640
641 if (max_size > (metaslab_min_alloc_size * 10))
642 return (B_FALSE);
643 return (B_TRUE);
644 }
645
646 static space_map_ops_t metaslab_cdf_ops = {
647 metaslab_pp_load,
648 metaslab_pp_unload,
649 metaslab_cdf_alloc,
650 metaslab_pp_claim,
651 metaslab_pp_free,
652 metaslab_pp_maxsize,
653 metaslab_cdf_fragmented
654 };
655
656 space_map_ops_t *zfs_metaslab_ops = &metaslab_cdf_ops;
657 #endif /* WITH_CDF_BLOCK_ALLOCATOR */
658
659 #if defined(WITH_NDF_BLOCK_ALLOCATOR)
660 uint64_t metaslab_ndf_clump_shift = 4;
661
662 static uint64_t
663 metaslab_ndf_alloc(space_map_t *sm, uint64_t size)
664 {
665 avl_tree_t *t = &sm->sm_root;
666 avl_index_t where;
667 space_seg_t *ss, ssearch;
668 uint64_t hbit = highbit(size);
669 uint64_t *cursor = (uint64_t *)sm->sm_ppd + hbit - 1;
670 uint64_t max_size = metaslab_pp_maxsize(sm);
671
672 ASSERT(MUTEX_HELD(sm->sm_lock));
673 ASSERT3U(avl_numnodes(&sm->sm_root), ==, avl_numnodes(sm->sm_pp_root));
674
675 if (max_size < size)
676 return (-1ULL);
677
678 ssearch.ss_start = *cursor;
679 ssearch.ss_end = *cursor + size;
680
681 ss = avl_find(t, &ssearch, &where);
682 if (ss == NULL || (ss->ss_start + size > ss->ss_end)) {
683 t = sm->sm_pp_root;
684
685 ssearch.ss_start = 0;
686 ssearch.ss_end = MIN(max_size,
687 1ULL << (hbit + metaslab_ndf_clump_shift));
688 ss = avl_find(t, &ssearch, &where);
689 if (ss == NULL)
690 ss = avl_nearest(t, where, AVL_AFTER);
691 ASSERT(ss != NULL);
692 }
693
694 if (ss != NULL) {
695 if (ss->ss_start + size <= ss->ss_end) {
696 *cursor = ss->ss_start + size;
697 return (ss->ss_start);
698 }
699 }
700 return (-1ULL);
701 }
702
703 static boolean_t
704 metaslab_ndf_fragmented(space_map_t *sm)
705 {
706 uint64_t max_size = metaslab_pp_maxsize(sm);
707
708 if (max_size > (metaslab_min_alloc_size << metaslab_ndf_clump_shift))
709 return (B_FALSE);
710 return (B_TRUE);
711 }
712
713
714 static space_map_ops_t metaslab_ndf_ops = {
715 metaslab_pp_load,
716 metaslab_pp_unload,
717 metaslab_ndf_alloc,
718 metaslab_pp_claim,
719 metaslab_pp_free,
720 metaslab_pp_maxsize,
721 metaslab_ndf_fragmented
722 };
723
724 space_map_ops_t *zfs_metaslab_ops = &metaslab_ndf_ops;
725 #endif /* WITH_NDF_BLOCK_ALLOCATOR */
726
727 /*
728 * ==========================================================================
729 * Metaslabs
730 * ==========================================================================
731 */
732 metaslab_t *
733 metaslab_init(metaslab_group_t *mg, space_map_obj_t *smo,
734 uint64_t start, uint64_t size, uint64_t txg)
735 {
736 vdev_t *vd = mg->mg_vd;
737 metaslab_t *msp;
738
739 msp = kmem_zalloc(sizeof (metaslab_t), KM_PUSHPAGE);
740 mutex_init(&msp->ms_lock, NULL, MUTEX_DEFAULT, NULL);
741
742 msp->ms_smo_syncing = *smo;
743
744 /*
745 * We create the main space map here, but we don't create the
746 * allocmaps and freemaps until metaslab_sync_done(). This serves
747 * two purposes: it allows metaslab_sync_done() to detect the
748 * addition of new space; and for debugging, it ensures that we'd
749 * data fault on any attempt to use this metaslab before it's ready.
750 */
751 msp->ms_map = kmem_zalloc(sizeof (space_map_t), KM_PUSHPAGE);
752 space_map_create(msp->ms_map, start, size,
753 vd->vdev_ashift, &msp->ms_lock);
754
755 metaslab_group_add(mg, msp);
756
757 if (metaslab_debug && smo->smo_object != 0) {
758 mutex_enter(&msp->ms_lock);
759 VERIFY(space_map_load(msp->ms_map, mg->mg_class->mc_ops,
760 SM_FREE, smo, spa_meta_objset(vd->vdev_spa)) == 0);
761 mutex_exit(&msp->ms_lock);
762 }
763
764 /*
765 * If we're opening an existing pool (txg == 0) or creating
766 * a new one (txg == TXG_INITIAL), all space is available now.
767 * If we're adding space to an existing pool, the new space
768 * does not become available until after this txg has synced.
769 */
770 if (txg <= TXG_INITIAL)
771 metaslab_sync_done(msp, 0);
772
773 if (txg != 0) {
774 vdev_dirty(vd, 0, NULL, txg);
775 vdev_dirty(vd, VDD_METASLAB, msp, txg);
776 }
777
778 return (msp);
779 }
780
781 void
782 metaslab_fini(metaslab_t *msp)
783 {
784 metaslab_group_t *mg = msp->ms_group;
785 int t;
786
787 vdev_space_update(mg->mg_vd,
788 -msp->ms_smo.smo_alloc, 0, -msp->ms_map->sm_size);
789
790 metaslab_group_remove(mg, msp);
791
792 mutex_enter(&msp->ms_lock);
793
794 space_map_unload(msp->ms_map);
795 space_map_destroy(msp->ms_map);
796 kmem_free(msp->ms_map, sizeof (*msp->ms_map));
797
798 for (t = 0; t < TXG_SIZE; t++) {
799 space_map_destroy(msp->ms_allocmap[t]);
800 space_map_destroy(msp->ms_freemap[t]);
801 kmem_free(msp->ms_allocmap[t], sizeof (*msp->ms_allocmap[t]));
802 kmem_free(msp->ms_freemap[t], sizeof (*msp->ms_freemap[t]));
803 }
804
805 for (t = 0; t < TXG_DEFER_SIZE; t++) {
806 space_map_destroy(msp->ms_defermap[t]);
807 kmem_free(msp->ms_defermap[t], sizeof (*msp->ms_defermap[t]));
808 }
809
810 ASSERT0(msp->ms_deferspace);
811
812 mutex_exit(&msp->ms_lock);
813 mutex_destroy(&msp->ms_lock);
814
815 kmem_free(msp, sizeof (metaslab_t));
816 }
817
818 #define METASLAB_WEIGHT_PRIMARY (1ULL << 63)
819 #define METASLAB_WEIGHT_SECONDARY (1ULL << 62)
820 #define METASLAB_ACTIVE_MASK \
821 (METASLAB_WEIGHT_PRIMARY | METASLAB_WEIGHT_SECONDARY)
822
823 static uint64_t
824 metaslab_weight(metaslab_t *msp)
825 {
826 metaslab_group_t *mg = msp->ms_group;
827 space_map_t *sm = msp->ms_map;
828 space_map_obj_t *smo = &msp->ms_smo;
829 vdev_t *vd = mg->mg_vd;
830 uint64_t weight, space;
831
832 ASSERT(MUTEX_HELD(&msp->ms_lock));
833
834 /*
835 * This vdev is in the process of being removed so there is nothing
836 * for us to do here.
837 */
838 if (vd->vdev_removing) {
839 ASSERT0(smo->smo_alloc);
840 ASSERT0(vd->vdev_ms_shift);
841 return (0);
842 }
843
844 /*
845 * The baseline weight is the metaslab's free space.
846 */
847 space = sm->sm_size - smo->smo_alloc;
848 weight = space;
849
850 /*
851 * Modern disks have uniform bit density and constant angular velocity.
852 * Therefore, the outer recording zones are faster (higher bandwidth)
853 * than the inner zones by the ratio of outer to inner track diameter,
854 * which is typically around 2:1. We account for this by assigning
855 * higher weight to lower metaslabs (multiplier ranging from 2x to 1x).
856 * In effect, this means that we'll select the metaslab with the most
857 * free bandwidth rather than simply the one with the most free space.
858 */
859 weight = 2 * weight -
860 ((sm->sm_start >> vd->vdev_ms_shift) * weight) / vd->vdev_ms_count;
861 ASSERT(weight >= space && weight <= 2 * space);
862
863 /*
864 * For locality, assign higher weight to metaslabs which have
865 * a lower offset than what we've already activated.
866 */
867 if (sm->sm_start <= mg->mg_bonus_area)
868 weight *= (metaslab_smo_bonus_pct / 100);
869 ASSERT(weight >= space &&
870 weight <= 2 * (metaslab_smo_bonus_pct / 100) * space);
871
872 if (sm->sm_loaded && !sm->sm_ops->smop_fragmented(sm)) {
873 /*
874 * If this metaslab is one we're actively using, adjust its
875 * weight to make it preferable to any inactive metaslab so
876 * we'll polish it off.
877 */
878 weight |= (msp->ms_weight & METASLAB_ACTIVE_MASK);
879 }
880 return (weight);
881 }
882
883 static void
884 metaslab_prefetch(metaslab_group_t *mg)
885 {
886 spa_t *spa = mg->mg_vd->vdev_spa;
887 metaslab_t *msp;
888 avl_tree_t *t = &mg->mg_metaslab_tree;
889 int m;
890
891 mutex_enter(&mg->mg_lock);
892
893 /*
894 * Prefetch the next potential metaslabs
895 */
896 for (msp = avl_first(t), m = 0; msp; msp = AVL_NEXT(t, msp), m++) {
897 space_map_t *sm = msp->ms_map;
898 space_map_obj_t *smo = &msp->ms_smo;
899
900 /* If we have reached our prefetch limit then we're done */
901 if (m >= metaslab_prefetch_limit)
902 break;
903
904 if (!sm->sm_loaded && smo->smo_object != 0) {
905 mutex_exit(&mg->mg_lock);
906 dmu_prefetch(spa_meta_objset(spa), smo->smo_object,
907 0ULL, smo->smo_objsize);
908 mutex_enter(&mg->mg_lock);
909 }
910 }
911 mutex_exit(&mg->mg_lock);
912 }
913
914 static int
915 metaslab_activate(metaslab_t *msp, uint64_t activation_weight)
916 {
917 metaslab_group_t *mg = msp->ms_group;
918 space_map_t *sm = msp->ms_map;
919 space_map_ops_t *sm_ops = msp->ms_group->mg_class->mc_ops;
920 int t;
921
922 ASSERT(MUTEX_HELD(&msp->ms_lock));
923
924 if ((msp->ms_weight & METASLAB_ACTIVE_MASK) == 0) {
925 space_map_load_wait(sm);
926 if (!sm->sm_loaded) {
927 space_map_obj_t *smo = &msp->ms_smo;
928
929 int error = space_map_load(sm, sm_ops, SM_FREE, smo,
930 spa_meta_objset(msp->ms_group->mg_vd->vdev_spa));
931 if (error) {
932 metaslab_group_sort(msp->ms_group, msp, 0);
933 return (error);
934 }
935 for (t = 0; t < TXG_DEFER_SIZE; t++)
936 space_map_walk(msp->ms_defermap[t],
937 space_map_claim, sm);
938
939 }
940
941 /*
942 * Track the bonus area as we activate new metaslabs.
943 */
944 if (sm->sm_start > mg->mg_bonus_area) {
945 mutex_enter(&mg->mg_lock);
946 mg->mg_bonus_area = sm->sm_start;
947 mutex_exit(&mg->mg_lock);
948 }
949
950 metaslab_group_sort(msp->ms_group, msp,
951 msp->ms_weight | activation_weight);
952 }
953 ASSERT(sm->sm_loaded);
954 ASSERT(msp->ms_weight & METASLAB_ACTIVE_MASK);
955
956 return (0);
957 }
958
959 static void
960 metaslab_passivate(metaslab_t *msp, uint64_t size)
961 {
962 /*
963 * If size < SPA_MINBLOCKSIZE, then we will not allocate from
964 * this metaslab again. In that case, it had better be empty,
965 * or we would be leaving space on the table.
966 */
967 ASSERT(size >= SPA_MINBLOCKSIZE || msp->ms_map->sm_space == 0);
968 metaslab_group_sort(msp->ms_group, msp, MIN(msp->ms_weight, size));
969 ASSERT((msp->ms_weight & METASLAB_ACTIVE_MASK) == 0);
970 }
971
972 /*
973 * Determine if the in-core space map representation can be condensed on-disk.
974 * We would like to use the following criteria to make our decision:
975 *
976 * 1. The size of the space map object should not dramatically increase as a
977 * result of writing out our in-core free map.
978 *
979 * 2. The minimal on-disk space map representation is zfs_condense_pct/100
980 * times the size than the in-core representation (i.e. zfs_condense_pct = 110
981 * and in-core = 1MB, minimal = 1.1.MB).
982 *
983 * Checking the first condition is tricky since we don't want to walk
984 * the entire AVL tree calculating the estimated on-disk size. Instead we
985 * use the size-ordered AVL tree in the space map and calculate the
986 * size required for the largest segment in our in-core free map. If the
987 * size required to represent that segment on disk is larger than the space
988 * map object then we avoid condensing this map.
989 *
990 * To determine the second criterion we use a best-case estimate and assume
991 * each segment can be represented on-disk as a single 64-bit entry. We refer
992 * to this best-case estimate as the space map's minimal form.
993 */
994 static boolean_t
995 metaslab_should_condense(metaslab_t *msp)
996 {
997 space_map_t *sm = msp->ms_map;
998 space_map_obj_t *smo = &msp->ms_smo_syncing;
999 space_seg_t *ss;
1000 uint64_t size, entries, segsz;
1001
1002 ASSERT(MUTEX_HELD(&msp->ms_lock));
1003 ASSERT(sm->sm_loaded);
1004
1005 /*
1006 * Use the sm_pp_root AVL tree, which is ordered by size, to obtain
1007 * the largest segment in the in-core free map. If the tree is
1008 * empty then we should condense the map.
1009 */
1010 ss = avl_last(sm->sm_pp_root);
1011 if (ss == NULL)
1012 return (B_TRUE);
1013
1014 /*
1015 * Calculate the number of 64-bit entries this segment would
1016 * require when written to disk. If this single segment would be
1017 * larger on-disk than the entire current on-disk structure, then
1018 * clearly condensing will increase the on-disk structure size.
1019 */
1020 size = (ss->ss_end - ss->ss_start) >> sm->sm_shift;
1021 entries = size / (MIN(size, SM_RUN_MAX));
1022 segsz = entries * sizeof (uint64_t);
1023
1024 return (segsz <= smo->smo_objsize &&
1025 smo->smo_objsize >= (zfs_condense_pct *
1026 sizeof (uint64_t) * avl_numnodes(&sm->sm_root)) / 100);
1027 }
1028
1029 /*
1030 * Condense the on-disk space map representation to its minimized form.
1031 * The minimized form consists of a small number of allocations followed by
1032 * the in-core free map.
1033 */
1034 static void
1035 metaslab_condense(metaslab_t *msp, uint64_t txg, dmu_tx_t *tx)
1036 {
1037 spa_t *spa = msp->ms_group->mg_vd->vdev_spa;
1038 space_map_t *freemap = msp->ms_freemap[txg & TXG_MASK];
1039 space_map_t condense_map;
1040 space_map_t *sm = msp->ms_map;
1041 objset_t *mos = spa_meta_objset(spa);
1042 space_map_obj_t *smo = &msp->ms_smo_syncing;
1043 int t;
1044
1045 ASSERT(MUTEX_HELD(&msp->ms_lock));
1046 ASSERT3U(spa_sync_pass(spa), ==, 1);
1047 ASSERT(sm->sm_loaded);
1048
1049 spa_dbgmsg(spa, "condensing: txg %llu, msp[%llu] %p, "
1050 "smo size %llu, segments %lu", txg,
1051 (msp->ms_map->sm_start / msp->ms_map->sm_size), msp,
1052 smo->smo_objsize, avl_numnodes(&sm->sm_root));
1053
1054 /*
1055 * Create an map that is a 100% allocated map. We remove segments
1056 * that have been freed in this txg, any deferred frees that exist,
1057 * and any allocation in the future. Removing segments should be
1058 * a relatively inexpensive operation since we expect these maps to
1059 * a small number of nodes.
1060 */
1061 space_map_create(&condense_map, sm->sm_start, sm->sm_size,
1062 sm->sm_shift, sm->sm_lock);
1063 space_map_add(&condense_map, condense_map.sm_start,
1064 condense_map.sm_size);
1065
1066 /*
1067 * Remove what's been freed in this txg from the condense_map.
1068 * Since we're in sync_pass 1, we know that all the frees from
1069 * this txg are in the freemap.
1070 */
1071 space_map_walk(freemap, space_map_remove, &condense_map);
1072
1073 for (t = 0; t < TXG_DEFER_SIZE; t++)
1074 space_map_walk(msp->ms_defermap[t],
1075 space_map_remove, &condense_map);
1076
1077 for (t = 1; t < TXG_CONCURRENT_STATES; t++)
1078 space_map_walk(msp->ms_allocmap[(txg + t) & TXG_MASK],
1079 space_map_remove, &condense_map);
1080
1081 /*
1082 * We're about to drop the metaslab's lock thus allowing
1083 * other consumers to change it's content. Set the
1084 * space_map's sm_condensing flag to ensure that
1085 * allocations on this metaslab do not occur while we're
1086 * in the middle of committing it to disk. This is only critical
1087 * for the ms_map as all other space_maps use per txg
1088 * views of their content.
1089 */
1090 sm->sm_condensing = B_TRUE;
1091
1092 mutex_exit(&msp->ms_lock);
1093 space_map_truncate(smo, mos, tx);
1094 mutex_enter(&msp->ms_lock);
1095
1096 /*
1097 * While we would ideally like to create a space_map representation
1098 * that consists only of allocation records, doing so can be
1099 * prohibitively expensive because the in-core free map can be
1100 * large, and therefore computationally expensive to subtract
1101 * from the condense_map. Instead we sync out two maps, a cheap
1102 * allocation only map followed by the in-core free map. While not
1103 * optimal, this is typically close to optimal, and much cheaper to
1104 * compute.
1105 */
1106 space_map_sync(&condense_map, SM_ALLOC, smo, mos, tx);
1107 space_map_vacate(&condense_map, NULL, NULL);
1108 space_map_destroy(&condense_map);
1109
1110 space_map_sync(sm, SM_FREE, smo, mos, tx);
1111 sm->sm_condensing = B_FALSE;
1112
1113 spa_dbgmsg(spa, "condensed: txg %llu, msp[%llu] %p, "
1114 "smo size %llu", txg,
1115 (msp->ms_map->sm_start / msp->ms_map->sm_size), msp,
1116 smo->smo_objsize);
1117 }
1118
1119 /*
1120 * Write a metaslab to disk in the context of the specified transaction group.
1121 */
1122 void
1123 metaslab_sync(metaslab_t *msp, uint64_t txg)
1124 {
1125 vdev_t *vd = msp->ms_group->mg_vd;
1126 spa_t *spa = vd->vdev_spa;
1127 objset_t *mos = spa_meta_objset(spa);
1128 space_map_t *allocmap = msp->ms_allocmap[txg & TXG_MASK];
1129 space_map_t **freemap = &msp->ms_freemap[txg & TXG_MASK];
1130 space_map_t **freed_map = &msp->ms_freemap[TXG_CLEAN(txg) & TXG_MASK];
1131 space_map_t *sm = msp->ms_map;
1132 space_map_obj_t *smo = &msp->ms_smo_syncing;
1133 dmu_buf_t *db;
1134 dmu_tx_t *tx;
1135
1136 ASSERT(!vd->vdev_ishole);
1137
1138 /*
1139 * This metaslab has just been added so there's no work to do now.
1140 */
1141 if (*freemap == NULL) {
1142 ASSERT3P(allocmap, ==, NULL);
1143 return;
1144 }
1145
1146 ASSERT3P(allocmap, !=, NULL);
1147 ASSERT3P(*freemap, !=, NULL);
1148 ASSERT3P(*freed_map, !=, NULL);
1149
1150 if (allocmap->sm_space == 0 && (*freemap)->sm_space == 0)
1151 return;
1152
1153 /*
1154 * The only state that can actually be changing concurrently with
1155 * metaslab_sync() is the metaslab's ms_map. No other thread can
1156 * be modifying this txg's allocmap, freemap, freed_map, or smo.
1157 * Therefore, we only hold ms_lock to satify space_map ASSERTs.
1158 * We drop it whenever we call into the DMU, because the DMU
1159 * can call down to us (e.g. via zio_free()) at any time.
1160 */
1161
1162 tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
1163
1164 if (smo->smo_object == 0) {
1165 ASSERT(smo->smo_objsize == 0);
1166 ASSERT(smo->smo_alloc == 0);
1167 smo->smo_object = dmu_object_alloc(mos,
1168 DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1169 DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1170 ASSERT(smo->smo_object != 0);
1171 dmu_write(mos, vd->vdev_ms_array, sizeof (uint64_t) *
1172 (sm->sm_start >> vd->vdev_ms_shift),
1173 sizeof (uint64_t), &smo->smo_object, tx);
1174 }
1175
1176 mutex_enter(&msp->ms_lock);
1177
1178 if (sm->sm_loaded && spa_sync_pass(spa) == 1 &&
1179 metaslab_should_condense(msp)) {
1180 metaslab_condense(msp, txg, tx);
1181 } else {
1182 space_map_sync(allocmap, SM_ALLOC, smo, mos, tx);
1183 space_map_sync(*freemap, SM_FREE, smo, mos, tx);
1184 }
1185
1186 space_map_vacate(allocmap, NULL, NULL);
1187
1188 /*
1189 * For sync pass 1, we avoid walking the entire space map and
1190 * instead will just swap the pointers for freemap and
1191 * freed_map. We can safely do this since the freed_map is
1192 * guaranteed to be empty on the initial pass.
1193 */
1194 if (spa_sync_pass(spa) == 1) {
1195 ASSERT0((*freed_map)->sm_space);
1196 ASSERT0(avl_numnodes(&(*freed_map)->sm_root));
1197 space_map_swap(freemap, freed_map);
1198 } else {
1199 space_map_vacate(*freemap, space_map_add, *freed_map);
1200 }
1201
1202 ASSERT0(msp->ms_allocmap[txg & TXG_MASK]->sm_space);
1203 ASSERT0(msp->ms_freemap[txg & TXG_MASK]->sm_space);
1204
1205 mutex_exit(&msp->ms_lock);
1206
1207 VERIFY0(dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1208 dmu_buf_will_dirty(db, tx);
1209 ASSERT3U(db->db_size, >=, sizeof (*smo));
1210 bcopy(smo, db->db_data, sizeof (*smo));
1211 dmu_buf_rele(db, FTAG);
1212
1213 dmu_tx_commit(tx);
1214 }
1215
1216 /*
1217 * Called after a transaction group has completely synced to mark
1218 * all of the metaslab's free space as usable.
1219 */
1220 void
1221 metaslab_sync_done(metaslab_t *msp, uint64_t txg)
1222 {
1223 space_map_obj_t *smo = &msp->ms_smo;
1224 space_map_obj_t *smosync = &msp->ms_smo_syncing;
1225 space_map_t *sm = msp->ms_map;
1226 space_map_t **freed_map = &msp->ms_freemap[TXG_CLEAN(txg) & TXG_MASK];
1227 space_map_t **defer_map = &msp->ms_defermap[txg % TXG_DEFER_SIZE];
1228 metaslab_group_t *mg = msp->ms_group;
1229 vdev_t *vd = mg->mg_vd;
1230 int64_t alloc_delta, defer_delta;
1231 int t;
1232
1233 ASSERT(!vd->vdev_ishole);
1234
1235 mutex_enter(&msp->ms_lock);
1236
1237 /*
1238 * If this metaslab is just becoming available, initialize its
1239 * allocmaps, freemaps, and defermap and add its capacity to the vdev.
1240 */
1241 if (*freed_map == NULL) {
1242 ASSERT(*defer_map == NULL);
1243 for (t = 0; t < TXG_SIZE; t++) {
1244 msp->ms_allocmap[t] = kmem_zalloc(sizeof (space_map_t),
1245 KM_PUSHPAGE);
1246 space_map_create(msp->ms_allocmap[t], sm->sm_start,
1247 sm->sm_size, sm->sm_shift, sm->sm_lock);
1248 msp->ms_freemap[t] = kmem_zalloc(sizeof (space_map_t),
1249 KM_PUSHPAGE);
1250 space_map_create(msp->ms_freemap[t], sm->sm_start,
1251 sm->sm_size, sm->sm_shift, sm->sm_lock);
1252 }
1253
1254 for (t = 0; t < TXG_DEFER_SIZE; t++) {
1255 msp->ms_defermap[t] = kmem_zalloc(sizeof (space_map_t),
1256 KM_PUSHPAGE);
1257 space_map_create(msp->ms_defermap[t], sm->sm_start,
1258 sm->sm_size, sm->sm_shift, sm->sm_lock);
1259 }
1260
1261 freed_map = &msp->ms_freemap[TXG_CLEAN(txg) & TXG_MASK];
1262 defer_map = &msp->ms_defermap[txg % TXG_DEFER_SIZE];
1263
1264 vdev_space_update(vd, 0, 0, sm->sm_size);
1265 }
1266
1267 alloc_delta = smosync->smo_alloc - smo->smo_alloc;
1268 defer_delta = (*freed_map)->sm_space - (*defer_map)->sm_space;
1269
1270 vdev_space_update(vd, alloc_delta + defer_delta, defer_delta, 0);
1271
1272 ASSERT(msp->ms_allocmap[txg & TXG_MASK]->sm_space == 0);
1273 ASSERT(msp->ms_freemap[txg & TXG_MASK]->sm_space == 0);
1274
1275 /*
1276 * If there's a space_map_load() in progress, wait for it to complete
1277 * so that we have a consistent view of the in-core space map.
1278 */
1279 space_map_load_wait(sm);
1280
1281 /*
1282 * Move the frees from the defer_map to this map (if it's loaded).
1283 * Swap the freed_map and the defer_map -- this is safe to do
1284 * because we've just emptied out the defer_map.
1285 */
1286 space_map_vacate(*defer_map, sm->sm_loaded ? space_map_free : NULL, sm);
1287 ASSERT0((*defer_map)->sm_space);
1288 ASSERT0(avl_numnodes(&(*defer_map)->sm_root));
1289 space_map_swap(freed_map, defer_map);
1290
1291 *smo = *smosync;
1292
1293 msp->ms_deferspace += defer_delta;
1294 ASSERT3S(msp->ms_deferspace, >=, 0);
1295 ASSERT3S(msp->ms_deferspace, <=, sm->sm_size);
1296 if (msp->ms_deferspace != 0) {
1297 /*
1298 * Keep syncing this metaslab until all deferred frees
1299 * are back in circulation.
1300 */
1301 vdev_dirty(vd, VDD_METASLAB, msp, txg + 1);
1302 }
1303
1304 /*
1305 * If the map is loaded but no longer active, evict it as soon as all
1306 * future allocations have synced. (If we unloaded it now and then
1307 * loaded a moment later, the map wouldn't reflect those allocations.)
1308 */
1309 if (sm->sm_loaded && (msp->ms_weight & METASLAB_ACTIVE_MASK) == 0) {
1310 int evictable = 1;
1311
1312 for (t = 1; t < TXG_CONCURRENT_STATES; t++)
1313 if (msp->ms_allocmap[(txg + t) & TXG_MASK]->sm_space)
1314 evictable = 0;
1315
1316 if (evictable && !metaslab_debug)
1317 space_map_unload(sm);
1318 }
1319
1320 metaslab_group_sort(mg, msp, metaslab_weight(msp));
1321
1322 mutex_exit(&msp->ms_lock);
1323 }
1324
1325 void
1326 metaslab_sync_reassess(metaslab_group_t *mg)
1327 {
1328 vdev_t *vd = mg->mg_vd;
1329 int64_t failures = mg->mg_alloc_failures;
1330 int m;
1331
1332 /*
1333 * Re-evaluate all metaslabs which have lower offsets than the
1334 * bonus area.
1335 */
1336 for (m = 0; m < vd->vdev_ms_count; m++) {
1337 metaslab_t *msp = vd->vdev_ms[m];
1338
1339 if (msp->ms_map->sm_start > mg->mg_bonus_area)
1340 break;
1341
1342 mutex_enter(&msp->ms_lock);
1343 metaslab_group_sort(mg, msp, metaslab_weight(msp));
1344 mutex_exit(&msp->ms_lock);
1345 }
1346
1347 atomic_add_64(&mg->mg_alloc_failures, -failures);
1348
1349 /*
1350 * Prefetch the next potential metaslabs
1351 */
1352 metaslab_prefetch(mg);
1353 }
1354
1355 static uint64_t
1356 metaslab_distance(metaslab_t *msp, dva_t *dva)
1357 {
1358 uint64_t ms_shift = msp->ms_group->mg_vd->vdev_ms_shift;
1359 uint64_t offset = DVA_GET_OFFSET(dva) >> ms_shift;
1360 uint64_t start = msp->ms_map->sm_start >> ms_shift;
1361
1362 if (msp->ms_group->mg_vd->vdev_id != DVA_GET_VDEV(dva))
1363 return (1ULL << 63);
1364
1365 if (offset < start)
1366 return ((start - offset) << ms_shift);
1367 if (offset > start)
1368 return ((offset - start) << ms_shift);
1369 return (0);
1370 }
1371
1372 static uint64_t
1373 metaslab_group_alloc(metaslab_group_t *mg, uint64_t psize, uint64_t asize,
1374 uint64_t txg, uint64_t min_distance, dva_t *dva, int d, int flags)
1375 {
1376 spa_t *spa = mg->mg_vd->vdev_spa;
1377 metaslab_t *msp = NULL;
1378 uint64_t offset = -1ULL;
1379 avl_tree_t *t = &mg->mg_metaslab_tree;
1380 uint64_t activation_weight;
1381 uint64_t target_distance;
1382 int i;
1383
1384 activation_weight = METASLAB_WEIGHT_PRIMARY;
1385 for (i = 0; i < d; i++) {
1386 if (DVA_GET_VDEV(&dva[i]) == mg->mg_vd->vdev_id) {
1387 activation_weight = METASLAB_WEIGHT_SECONDARY;
1388 break;
1389 }
1390 }
1391
1392 for (;;) {
1393 boolean_t was_active;
1394
1395 mutex_enter(&mg->mg_lock);
1396 for (msp = avl_first(t); msp; msp = AVL_NEXT(t, msp)) {
1397 if (msp->ms_weight < asize) {
1398 spa_dbgmsg(spa, "%s: failed to meet weight "
1399 "requirement: vdev %llu, txg %llu, mg %p, "
1400 "msp %p, psize %llu, asize %llu, "
1401 "failures %llu, weight %llu",
1402 spa_name(spa), mg->mg_vd->vdev_id, txg,
1403 mg, msp, psize, asize,
1404 mg->mg_alloc_failures, msp->ms_weight);
1405 mutex_exit(&mg->mg_lock);
1406 return (-1ULL);
1407 }
1408
1409 /*
1410 * If the selected metaslab is condensing, skip it.
1411 */
1412 if (msp->ms_map->sm_condensing)
1413 continue;
1414
1415 was_active = msp->ms_weight & METASLAB_ACTIVE_MASK;
1416 if (activation_weight == METASLAB_WEIGHT_PRIMARY)
1417 break;
1418
1419 target_distance = min_distance +
1420 (msp->ms_smo.smo_alloc ? 0 : min_distance >> 1);
1421
1422 for (i = 0; i < d; i++)
1423 if (metaslab_distance(msp, &dva[i]) <
1424 target_distance)
1425 break;
1426 if (i == d)
1427 break;
1428 }
1429 mutex_exit(&mg->mg_lock);
1430 if (msp == NULL)
1431 return (-1ULL);
1432
1433 /*
1434 * If we've already reached the allowable number of failed
1435 * allocation attempts on this metaslab group then we
1436 * consider skipping it. We skip it only if we're allowed
1437 * to "fast" gang, the physical size is larger than
1438 * a gang block, and we're attempting to allocate from
1439 * the primary metaslab.
1440 */
1441 if (mg->mg_alloc_failures > zfs_mg_alloc_failures &&
1442 CAN_FASTGANG(flags) && psize > SPA_GANGBLOCKSIZE &&
1443 activation_weight == METASLAB_WEIGHT_PRIMARY) {
1444 spa_dbgmsg(spa, "%s: skipping metaslab group: "
1445 "vdev %llu, txg %llu, mg %p, psize %llu, "
1446 "asize %llu, failures %llu", spa_name(spa),
1447 mg->mg_vd->vdev_id, txg, mg, psize, asize,
1448 mg->mg_alloc_failures);
1449 return (-1ULL);
1450 }
1451
1452 mutex_enter(&msp->ms_lock);
1453
1454 /*
1455 * Ensure that the metaslab we have selected is still
1456 * capable of handling our request. It's possible that
1457 * another thread may have changed the weight while we
1458 * were blocked on the metaslab lock.
1459 */
1460 if (msp->ms_weight < asize || (was_active &&
1461 !(msp->ms_weight & METASLAB_ACTIVE_MASK) &&
1462 activation_weight == METASLAB_WEIGHT_PRIMARY)) {
1463 mutex_exit(&msp->ms_lock);
1464 continue;
1465 }
1466
1467 if ((msp->ms_weight & METASLAB_WEIGHT_SECONDARY) &&
1468 activation_weight == METASLAB_WEIGHT_PRIMARY) {
1469 metaslab_passivate(msp,
1470 msp->ms_weight & ~METASLAB_ACTIVE_MASK);
1471 mutex_exit(&msp->ms_lock);
1472 continue;
1473 }
1474
1475 if (metaslab_activate(msp, activation_weight) != 0) {
1476 mutex_exit(&msp->ms_lock);
1477 continue;
1478 }
1479
1480 /*
1481 * If this metaslab is currently condensing then pick again as
1482 * we can't manipulate this metaslab until it's committed
1483 * to disk.
1484 */
1485 if (msp->ms_map->sm_condensing) {
1486 mutex_exit(&msp->ms_lock);
1487 continue;
1488 }
1489
1490 if ((offset = space_map_alloc(msp->ms_map, asize)) != -1ULL)
1491 break;
1492
1493 atomic_inc_64(&mg->mg_alloc_failures);
1494
1495 metaslab_passivate(msp, space_map_maxsize(msp->ms_map));
1496
1497 mutex_exit(&msp->ms_lock);
1498 }
1499
1500 if (msp->ms_allocmap[txg & TXG_MASK]->sm_space == 0)
1501 vdev_dirty(mg->mg_vd, VDD_METASLAB, msp, txg);
1502
1503 space_map_add(msp->ms_allocmap[txg & TXG_MASK], offset, asize);
1504
1505 mutex_exit(&msp->ms_lock);
1506
1507 return (offset);
1508 }
1509
1510 /*
1511 * Allocate a block for the specified i/o.
1512 */
1513 static int
1514 metaslab_alloc_dva(spa_t *spa, metaslab_class_t *mc, uint64_t psize,
1515 dva_t *dva, int d, dva_t *hintdva, uint64_t txg, int flags)
1516 {
1517 metaslab_group_t *mg, *fast_mg, *rotor;
1518 vdev_t *vd;
1519 int dshift = 3;
1520 int all_zero;
1521 int zio_lock = B_FALSE;
1522 boolean_t allocatable;
1523 uint64_t offset = -1ULL;
1524 uint64_t asize;
1525 uint64_t distance;
1526
1527 ASSERT(!DVA_IS_VALID(&dva[d]));
1528
1529 /*
1530 * For testing, make some blocks above a certain size be gang blocks.
1531 */
1532 if (psize >= metaslab_gang_bang && (ddi_get_lbolt() & 3) == 0)
1533 return (SET_ERROR(ENOSPC));
1534
1535 if (flags & METASLAB_FASTWRITE)
1536 mutex_enter(&mc->mc_fastwrite_lock);
1537
1538 /*
1539 * Start at the rotor and loop through all mgs until we find something.
1540 * Note that there's no locking on mc_rotor or mc_aliquot because
1541 * nothing actually breaks if we miss a few updates -- we just won't
1542 * allocate quite as evenly. It all balances out over time.
1543 *
1544 * If we are doing ditto or log blocks, try to spread them across
1545 * consecutive vdevs. If we're forced to reuse a vdev before we've
1546 * allocated all of our ditto blocks, then try and spread them out on
1547 * that vdev as much as possible. If it turns out to not be possible,
1548 * gradually lower our standards until anything becomes acceptable.
1549 * Also, allocating on consecutive vdevs (as opposed to random vdevs)
1550 * gives us hope of containing our fault domains to something we're
1551 * able to reason about. Otherwise, any two top-level vdev failures
1552 * will guarantee the loss of data. With consecutive allocation,
1553 * only two adjacent top-level vdev failures will result in data loss.
1554 *
1555 * If we are doing gang blocks (hintdva is non-NULL), try to keep
1556 * ourselves on the same vdev as our gang block header. That
1557 * way, we can hope for locality in vdev_cache, plus it makes our
1558 * fault domains something tractable.
1559 */
1560 if (hintdva) {
1561 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&hintdva[d]));
1562
1563 /*
1564 * It's possible the vdev we're using as the hint no
1565 * longer exists (i.e. removed). Consult the rotor when
1566 * all else fails.
1567 */
1568 if (vd != NULL) {
1569 mg = vd->vdev_mg;
1570
1571 if (flags & METASLAB_HINTBP_AVOID &&
1572 mg->mg_next != NULL)
1573 mg = mg->mg_next;
1574 } else {
1575 mg = mc->mc_rotor;
1576 }
1577 } else if (d != 0) {
1578 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d - 1]));
1579 mg = vd->vdev_mg->mg_next;
1580 } else if (flags & METASLAB_FASTWRITE) {
1581 mg = fast_mg = mc->mc_rotor;
1582
1583 do {
1584 if (fast_mg->mg_vd->vdev_pending_fastwrite <
1585 mg->mg_vd->vdev_pending_fastwrite)
1586 mg = fast_mg;
1587 } while ((fast_mg = fast_mg->mg_next) != mc->mc_rotor);
1588
1589 } else {
1590 mg = mc->mc_rotor;
1591 }
1592
1593 /*
1594 * If the hint put us into the wrong metaslab class, or into a
1595 * metaslab group that has been passivated, just follow the rotor.
1596 */
1597 if (mg->mg_class != mc || mg->mg_activation_count <= 0)
1598 mg = mc->mc_rotor;
1599
1600 rotor = mg;
1601 top:
1602 all_zero = B_TRUE;
1603 do {
1604 ASSERT(mg->mg_activation_count == 1);
1605
1606 vd = mg->mg_vd;
1607
1608 /*
1609 * Don't allocate from faulted devices.
1610 */
1611 if (zio_lock) {
1612 spa_config_enter(spa, SCL_ZIO, FTAG, RW_READER);
1613 allocatable = vdev_allocatable(vd);
1614 spa_config_exit(spa, SCL_ZIO, FTAG);
1615 } else {
1616 allocatable = vdev_allocatable(vd);
1617 }
1618 if (!allocatable)
1619 goto next;
1620
1621 /*
1622 * Avoid writing single-copy data to a failing vdev
1623 */
1624 if ((vd->vdev_stat.vs_write_errors > 0 ||
1625 vd->vdev_state < VDEV_STATE_HEALTHY) &&
1626 d == 0 && dshift == 3) {
1627 all_zero = B_FALSE;
1628 goto next;
1629 }
1630
1631 ASSERT(mg->mg_class == mc);
1632
1633 distance = vd->vdev_asize >> dshift;
1634 if (distance <= (1ULL << vd->vdev_ms_shift))
1635 distance = 0;
1636 else
1637 all_zero = B_FALSE;
1638
1639 asize = vdev_psize_to_asize(vd, psize);
1640 ASSERT(P2PHASE(asize, 1ULL << vd->vdev_ashift) == 0);
1641
1642 offset = metaslab_group_alloc(mg, psize, asize, txg, distance,
1643 dva, d, flags);
1644 if (offset != -1ULL) {
1645 /*
1646 * If we've just selected this metaslab group,
1647 * figure out whether the corresponding vdev is
1648 * over- or under-used relative to the pool,
1649 * and set an allocation bias to even it out.
1650 */
1651 if (mc->mc_aliquot == 0) {
1652 vdev_stat_t *vs = &vd->vdev_stat;
1653 int64_t vu, cu;
1654
1655 vu = (vs->vs_alloc * 100) / (vs->vs_space + 1);
1656 cu = (mc->mc_alloc * 100) / (mc->mc_space + 1);
1657
1658 /*
1659 * Calculate how much more or less we should
1660 * try to allocate from this device during
1661 * this iteration around the rotor.
1662 * For example, if a device is 80% full
1663 * and the pool is 20% full then we should
1664 * reduce allocations by 60% on this device.
1665 *
1666 * mg_bias = (20 - 80) * 512K / 100 = -307K
1667 *
1668 * This reduces allocations by 307K for this
1669 * iteration.
1670 */
1671 mg->mg_bias = ((cu - vu) *
1672 (int64_t)mg->mg_aliquot) / 100;
1673 }
1674
1675 if ((flags & METASLAB_FASTWRITE) ||
1676 atomic_add_64_nv(&mc->mc_aliquot, asize) >=
1677 mg->mg_aliquot + mg->mg_bias) {
1678 mc->mc_rotor = mg->mg_next;
1679 mc->mc_aliquot = 0;
1680 }
1681
1682 DVA_SET_VDEV(&dva[d], vd->vdev_id);
1683 DVA_SET_OFFSET(&dva[d], offset);
1684 DVA_SET_GANG(&dva[d], !!(flags & METASLAB_GANG_HEADER));
1685 DVA_SET_ASIZE(&dva[d], asize);
1686
1687 if (flags & METASLAB_FASTWRITE) {
1688 atomic_add_64(&vd->vdev_pending_fastwrite,
1689 psize);
1690 mutex_exit(&mc->mc_fastwrite_lock);
1691 }
1692
1693 return (0);
1694 }
1695 next:
1696 mc->mc_rotor = mg->mg_next;
1697 mc->mc_aliquot = 0;
1698 } while ((mg = mg->mg_next) != rotor);
1699
1700 if (!all_zero) {
1701 dshift++;
1702 ASSERT(dshift < 64);
1703 goto top;
1704 }
1705
1706 if (!allocatable && !zio_lock) {
1707 dshift = 3;
1708 zio_lock = B_TRUE;
1709 goto top;
1710 }
1711
1712 bzero(&dva[d], sizeof (dva_t));
1713
1714 if (flags & METASLAB_FASTWRITE)
1715 mutex_exit(&mc->mc_fastwrite_lock);
1716
1717 return (SET_ERROR(ENOSPC));
1718 }
1719
1720 /*
1721 * Free the block represented by DVA in the context of the specified
1722 * transaction group.
1723 */
1724 static void
1725 metaslab_free_dva(spa_t *spa, const dva_t *dva, uint64_t txg, boolean_t now)
1726 {
1727 uint64_t vdev = DVA_GET_VDEV(dva);
1728 uint64_t offset = DVA_GET_OFFSET(dva);
1729 uint64_t size = DVA_GET_ASIZE(dva);
1730 vdev_t *vd;
1731 metaslab_t *msp;
1732
1733 ASSERT(DVA_IS_VALID(dva));
1734
1735 if (txg > spa_freeze_txg(spa))
1736 return;
1737
1738 if ((vd = vdev_lookup_top(spa, vdev)) == NULL ||
1739 (offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count) {
1740 cmn_err(CE_WARN, "metaslab_free_dva(): bad DVA %llu:%llu",
1741 (u_longlong_t)vdev, (u_longlong_t)offset);
1742 ASSERT(0);
1743 return;
1744 }
1745
1746 msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
1747
1748 if (DVA_GET_GANG(dva))
1749 size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
1750
1751 mutex_enter(&msp->ms_lock);
1752
1753 if (now) {
1754 space_map_remove(msp->ms_allocmap[txg & TXG_MASK],
1755 offset, size);
1756 space_map_free(msp->ms_map, offset, size);
1757 } else {
1758 if (msp->ms_freemap[txg & TXG_MASK]->sm_space == 0)
1759 vdev_dirty(vd, VDD_METASLAB, msp, txg);
1760 space_map_add(msp->ms_freemap[txg & TXG_MASK], offset, size);
1761 }
1762
1763 mutex_exit(&msp->ms_lock);
1764 }
1765
1766 /*
1767 * Intent log support: upon opening the pool after a crash, notify the SPA
1768 * of blocks that the intent log has allocated for immediate write, but
1769 * which are still considered free by the SPA because the last transaction
1770 * group didn't commit yet.
1771 */
1772 static int
1773 metaslab_claim_dva(spa_t *spa, const dva_t *dva, uint64_t txg)
1774 {
1775 uint64_t vdev = DVA_GET_VDEV(dva);
1776 uint64_t offset = DVA_GET_OFFSET(dva);
1777 uint64_t size = DVA_GET_ASIZE(dva);
1778 vdev_t *vd;
1779 metaslab_t *msp;
1780 int error = 0;
1781
1782 ASSERT(DVA_IS_VALID(dva));
1783
1784 if ((vd = vdev_lookup_top(spa, vdev)) == NULL ||
1785 (offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count)
1786 return (SET_ERROR(ENXIO));
1787
1788 msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
1789
1790 if (DVA_GET_GANG(dva))
1791 size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
1792
1793 mutex_enter(&msp->ms_lock);
1794
1795 if ((txg != 0 && spa_writeable(spa)) || !msp->ms_map->sm_loaded)
1796 error = metaslab_activate(msp, METASLAB_WEIGHT_SECONDARY);
1797
1798 if (error == 0 && !space_map_contains(msp->ms_map, offset, size))
1799 error = SET_ERROR(ENOENT);
1800
1801 if (error || txg == 0) { /* txg == 0 indicates dry run */
1802 mutex_exit(&msp->ms_lock);
1803 return (error);
1804 }
1805
1806 space_map_claim(msp->ms_map, offset, size);
1807
1808 if (spa_writeable(spa)) { /* don't dirty if we're zdb(1M) */
1809 if (msp->ms_allocmap[txg & TXG_MASK]->sm_space == 0)
1810 vdev_dirty(vd, VDD_METASLAB, msp, txg);
1811 space_map_add(msp->ms_allocmap[txg & TXG_MASK], offset, size);
1812 }
1813
1814 mutex_exit(&msp->ms_lock);
1815
1816 return (0);
1817 }
1818
1819 int
1820 metaslab_alloc(spa_t *spa, metaslab_class_t *mc, uint64_t psize, blkptr_t *bp,
1821 int ndvas, uint64_t txg, blkptr_t *hintbp, int flags)
1822 {
1823 dva_t *dva = bp->blk_dva;
1824 dva_t *hintdva = hintbp->blk_dva;
1825 int d, error = 0;
1826
1827 ASSERT(bp->blk_birth == 0);
1828 ASSERT(BP_PHYSICAL_BIRTH(bp) == 0);
1829
1830 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_READER);
1831
1832 if (mc->mc_rotor == NULL) { /* no vdevs in this class */
1833 spa_config_exit(spa, SCL_ALLOC, FTAG);
1834 return (SET_ERROR(ENOSPC));
1835 }
1836
1837 ASSERT(ndvas > 0 && ndvas <= spa_max_replication(spa));
1838 ASSERT(BP_GET_NDVAS(bp) == 0);
1839 ASSERT(hintbp == NULL || ndvas <= BP_GET_NDVAS(hintbp));
1840
1841 for (d = 0; d < ndvas; d++) {
1842 error = metaslab_alloc_dva(spa, mc, psize, dva, d, hintdva,
1843 txg, flags);
1844 if (error) {
1845 for (d--; d >= 0; d--) {
1846 metaslab_free_dva(spa, &dva[d], txg, B_TRUE);
1847 bzero(&dva[d], sizeof (dva_t));
1848 }
1849 spa_config_exit(spa, SCL_ALLOC, FTAG);
1850 return (error);
1851 }
1852 }
1853 ASSERT(error == 0);
1854 ASSERT(BP_GET_NDVAS(bp) == ndvas);
1855
1856 spa_config_exit(spa, SCL_ALLOC, FTAG);
1857
1858 BP_SET_BIRTH(bp, txg, txg);
1859
1860 return (0);
1861 }
1862
1863 void
1864 metaslab_free(spa_t *spa, const blkptr_t *bp, uint64_t txg, boolean_t now)
1865 {
1866 const dva_t *dva = bp->blk_dva;
1867 int d, ndvas = BP_GET_NDVAS(bp);
1868
1869 ASSERT(!BP_IS_HOLE(bp));
1870 ASSERT(!now || bp->blk_birth >= spa_syncing_txg(spa));
1871
1872 spa_config_enter(spa, SCL_FREE, FTAG, RW_READER);
1873
1874 for (d = 0; d < ndvas; d++)
1875 metaslab_free_dva(spa, &dva[d], txg, now);
1876
1877 spa_config_exit(spa, SCL_FREE, FTAG);
1878 }
1879
1880 int
1881 metaslab_claim(spa_t *spa, const blkptr_t *bp, uint64_t txg)
1882 {
1883 const dva_t *dva = bp->blk_dva;
1884 int ndvas = BP_GET_NDVAS(bp);
1885 int d, error = 0;
1886
1887 ASSERT(!BP_IS_HOLE(bp));
1888
1889 if (txg != 0) {
1890 /*
1891 * First do a dry run to make sure all DVAs are claimable,
1892 * so we don't have to unwind from partial failures below.
1893 */
1894 if ((error = metaslab_claim(spa, bp, 0)) != 0)
1895 return (error);
1896 }
1897
1898 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_READER);
1899
1900 for (d = 0; d < ndvas; d++)
1901 if ((error = metaslab_claim_dva(spa, &dva[d], txg)) != 0)
1902 break;
1903
1904 spa_config_exit(spa, SCL_ALLOC, FTAG);
1905
1906 ASSERT(error == 0 || txg == 0);
1907
1908 return (error);
1909 }
1910
1911 void metaslab_fastwrite_mark(spa_t *spa, const blkptr_t *bp)
1912 {
1913 const dva_t *dva = bp->blk_dva;
1914 int ndvas = BP_GET_NDVAS(bp);
1915 uint64_t psize = BP_GET_PSIZE(bp);
1916 int d;
1917 vdev_t *vd;
1918
1919 ASSERT(!BP_IS_HOLE(bp));
1920 ASSERT(psize > 0);
1921
1922 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1923
1924 for (d = 0; d < ndvas; d++) {
1925 if ((vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d]))) == NULL)
1926 continue;
1927 atomic_add_64(&vd->vdev_pending_fastwrite, psize);
1928 }
1929
1930 spa_config_exit(spa, SCL_VDEV, FTAG);
1931 }
1932
1933 void metaslab_fastwrite_unmark(spa_t *spa, const blkptr_t *bp)
1934 {
1935 const dva_t *dva = bp->blk_dva;
1936 int ndvas = BP_GET_NDVAS(bp);
1937 uint64_t psize = BP_GET_PSIZE(bp);
1938 int d;
1939 vdev_t *vd;
1940
1941 ASSERT(!BP_IS_HOLE(bp));
1942 ASSERT(psize > 0);
1943
1944 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1945
1946 for (d = 0; d < ndvas; d++) {
1947 if ((vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d]))) == NULL)
1948 continue;
1949 ASSERT3U(vd->vdev_pending_fastwrite, >=, psize);
1950 atomic_sub_64(&vd->vdev_pending_fastwrite, psize);
1951 }
1952
1953 spa_config_exit(spa, SCL_VDEV, FTAG);
1954 }
1955
1956 static void
1957 checkmap(space_map_t *sm, uint64_t off, uint64_t size)
1958 {
1959 space_seg_t *ss;
1960 avl_index_t where;
1961
1962 mutex_enter(sm->sm_lock);
1963 ss = space_map_find(sm, off, size, &where);
1964 if (ss != NULL)
1965 panic("freeing free block; ss=%p", (void *)ss);
1966 mutex_exit(sm->sm_lock);
1967 }
1968
1969 void
1970 metaslab_check_free(spa_t *spa, const blkptr_t *bp)
1971 {
1972 int i, j;
1973
1974 if ((zfs_flags & ZFS_DEBUG_ZIO_FREE) == 0)
1975 return;
1976
1977 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1978 for (i = 0; i < BP_GET_NDVAS(bp); i++) {
1979 uint64_t vdid = DVA_GET_VDEV(&bp->blk_dva[i]);
1980 vdev_t *vd = vdev_lookup_top(spa, vdid);
1981 uint64_t off = DVA_GET_OFFSET(&bp->blk_dva[i]);
1982 uint64_t size = DVA_GET_ASIZE(&bp->blk_dva[i]);
1983 metaslab_t *ms = vd->vdev_ms[off >> vd->vdev_ms_shift];
1984
1985 if (ms->ms_map->sm_loaded)
1986 checkmap(ms->ms_map, off, size);
1987
1988 for (j = 0; j < TXG_SIZE; j++)
1989 checkmap(ms->ms_freemap[j], off, size);
1990 for (j = 0; j < TXG_DEFER_SIZE; j++)
1991 checkmap(ms->ms_defermap[j], off, size);
1992 }
1993 spa_config_exit(spa, SCL_VDEV, FTAG);
1994 }
1995
1996 #if defined(_KERNEL) && defined(HAVE_SPL)
1997 module_param(metaslab_debug, int, 0644);
1998 MODULE_PARM_DESC(metaslab_debug, "keep space maps in core to verify frees");
1999 #endif /* _KERNEL && HAVE_SPL */