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