]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - module/zfs/metaslab.c
Illumos #4730 destroy metaslab group taskq
[FreeBSD/FreeBSD.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 #include <sys/spa_impl.h>
35
36 #define WITH_DF_BLOCK_ALLOCATOR
37
38 /*
39  * Allow allocations to switch to gang blocks quickly. We do this to
40  * avoid having to load lots of space_maps in a given txg. There are,
41  * however, some cases where we want to avoid "fast" ganging and instead
42  * we want to do an exhaustive search of all metaslabs on this device.
43  * Currently we don't allow any gang, zil, or dump device related allocations
44  * to "fast" gang.
45  */
46 #define CAN_FASTGANG(flags) \
47         (!((flags) & (METASLAB_GANG_CHILD | METASLAB_GANG_HEADER | \
48         METASLAB_GANG_AVOID)))
49
50 #define METASLAB_WEIGHT_PRIMARY         (1ULL << 63)
51 #define METASLAB_WEIGHT_SECONDARY       (1ULL << 62)
52 #define METASLAB_ACTIVE_MASK            \
53         (METASLAB_WEIGHT_PRIMARY | METASLAB_WEIGHT_SECONDARY)
54
55 uint64_t metaslab_aliquot = 512ULL << 10;
56 uint64_t metaslab_gang_bang = SPA_MAXBLOCKSIZE + 1;     /* force gang blocks */
57
58 /*
59  * The in-core space map representation is more compact than its on-disk form.
60  * The zfs_condense_pct determines how much more compact the in-core
61  * space_map representation must be before we compact it on-disk.
62  * Values should be greater than or equal to 100.
63  */
64 int zfs_condense_pct = 200;
65
66 /*
67  * This value defines the number of allowed allocation failures per vdev.
68  * If a device reaches this threshold in a given txg then we consider skipping
69  * allocations on that device. The value of zfs_mg_alloc_failures is computed
70  * in zio_init() unless it has been overridden in /etc/system.
71  */
72 int zfs_mg_alloc_failures = 0;
73
74 /*
75  * The zfs_mg_noalloc_threshold defines which metaslab groups should
76  * be eligible for allocation. The value is defined as a percentage of
77  * a free space. Metaslab groups that have more free space than
78  * zfs_mg_noalloc_threshold are always eligible for allocations. Once
79  * a metaslab group's free space is less than or equal to the
80  * zfs_mg_noalloc_threshold the allocator will avoid allocating to that
81  * group unless all groups in the pool have reached zfs_mg_noalloc_threshold.
82  * Once all groups in the pool reach zfs_mg_noalloc_threshold then all
83  * groups are allowed to accept allocations. Gang blocks are always
84  * eligible to allocate on any metaslab group. The default value of 0 means
85  * no metaslab group will be excluded based on this criterion.
86  */
87 int zfs_mg_noalloc_threshold = 0;
88
89 /*
90  * When set will load all metaslabs when pool is first opened.
91  */
92 int metaslab_debug_load = 0;
93
94 /*
95  * When set will prevent metaslabs from being unloaded.
96  */
97 int metaslab_debug_unload = 0;
98
99 /*
100  * Minimum size which forces the dynamic allocator to change
101  * it's allocation strategy.  Once the space map cannot satisfy
102  * an allocation of this size then it switches to using more
103  * aggressive strategy (i.e search by size rather than offset).
104  */
105 uint64_t metaslab_df_alloc_threshold = SPA_MAXBLOCKSIZE;
106
107 /*
108  * The minimum free space, in percent, which must be available
109  * in a space map to continue allocations in a first-fit fashion.
110  * Once the space_map's free space drops below this level we dynamically
111  * switch to using best-fit allocations.
112  */
113 int metaslab_df_free_pct = 4;
114
115 /*
116  * A metaslab is considered "free" if it contains a contiguous
117  * segment which is greater than metaslab_min_alloc_size.
118  */
119 uint64_t metaslab_min_alloc_size = DMU_MAX_ACCESS;
120
121 /*
122  * Percentage of all cpus that can be used by the metaslab taskq.
123  */
124 int metaslab_load_pct = 50;
125
126 /*
127  * Determines how many txgs a metaslab may remain loaded without having any
128  * allocations from it. As long as a metaslab continues to be used we will
129  * keep it loaded.
130  */
131 int metaslab_unload_delay = TXG_SIZE * 2;
132
133 /*
134  * Should we be willing to write data to degraded vdevs?
135  */
136 boolean_t zfs_write_to_degraded = B_FALSE;
137
138 /*
139  * Max number of metaslabs per group to preload.
140  */
141 int metaslab_preload_limit = SPA_DVAS_PER_BP;
142
143 /*
144  * Enable/disable preloading of metaslab.
145  */
146 boolean_t metaslab_preload_enabled = B_TRUE;
147
148 /*
149  * Enable/disable additional weight factor for each metaslab.
150  */
151 boolean_t metaslab_weight_factor_enable = B_FALSE;
152
153
154 /*
155  * ==========================================================================
156  * Metaslab classes
157  * ==========================================================================
158  */
159 metaslab_class_t *
160 metaslab_class_create(spa_t *spa, metaslab_ops_t *ops)
161 {
162         metaslab_class_t *mc;
163
164         mc = kmem_zalloc(sizeof (metaslab_class_t), KM_PUSHPAGE);
165
166         mc->mc_spa = spa;
167         mc->mc_rotor = NULL;
168         mc->mc_ops = ops;
169         mutex_init(&mc->mc_fastwrite_lock, NULL, MUTEX_DEFAULT, NULL);
170
171         return (mc);
172 }
173
174 void
175 metaslab_class_destroy(metaslab_class_t *mc)
176 {
177         ASSERT(mc->mc_rotor == NULL);
178         ASSERT(mc->mc_alloc == 0);
179         ASSERT(mc->mc_deferred == 0);
180         ASSERT(mc->mc_space == 0);
181         ASSERT(mc->mc_dspace == 0);
182
183         mutex_destroy(&mc->mc_fastwrite_lock);
184         kmem_free(mc, sizeof (metaslab_class_t));
185 }
186
187 int
188 metaslab_class_validate(metaslab_class_t *mc)
189 {
190         metaslab_group_t *mg;
191         vdev_t *vd;
192
193         /*
194          * Must hold one of the spa_config locks.
195          */
196         ASSERT(spa_config_held(mc->mc_spa, SCL_ALL, RW_READER) ||
197             spa_config_held(mc->mc_spa, SCL_ALL, RW_WRITER));
198
199         if ((mg = mc->mc_rotor) == NULL)
200                 return (0);
201
202         do {
203                 vd = mg->mg_vd;
204                 ASSERT(vd->vdev_mg != NULL);
205                 ASSERT3P(vd->vdev_top, ==, vd);
206                 ASSERT3P(mg->mg_class, ==, mc);
207                 ASSERT3P(vd->vdev_ops, !=, &vdev_hole_ops);
208         } while ((mg = mg->mg_next) != mc->mc_rotor);
209
210         return (0);
211 }
212
213 void
214 metaslab_class_space_update(metaslab_class_t *mc, int64_t alloc_delta,
215     int64_t defer_delta, int64_t space_delta, int64_t dspace_delta)
216 {
217         atomic_add_64(&mc->mc_alloc, alloc_delta);
218         atomic_add_64(&mc->mc_deferred, defer_delta);
219         atomic_add_64(&mc->mc_space, space_delta);
220         atomic_add_64(&mc->mc_dspace, dspace_delta);
221 }
222
223 uint64_t
224 metaslab_class_get_alloc(metaslab_class_t *mc)
225 {
226         return (mc->mc_alloc);
227 }
228
229 uint64_t
230 metaslab_class_get_deferred(metaslab_class_t *mc)
231 {
232         return (mc->mc_deferred);
233 }
234
235 uint64_t
236 metaslab_class_get_space(metaslab_class_t *mc)
237 {
238         return (mc->mc_space);
239 }
240
241 uint64_t
242 metaslab_class_get_dspace(metaslab_class_t *mc)
243 {
244         return (spa_deflate(mc->mc_spa) ? mc->mc_dspace : mc->mc_space);
245 }
246
247 /*
248  * ==========================================================================
249  * Metaslab groups
250  * ==========================================================================
251  */
252 static int
253 metaslab_compare(const void *x1, const void *x2)
254 {
255         const metaslab_t *m1 = x1;
256         const metaslab_t *m2 = x2;
257
258         if (m1->ms_weight < m2->ms_weight)
259                 return (1);
260         if (m1->ms_weight > m2->ms_weight)
261                 return (-1);
262
263         /*
264          * If the weights are identical, use the offset to force uniqueness.
265          */
266         if (m1->ms_start < m2->ms_start)
267                 return (-1);
268         if (m1->ms_start > m2->ms_start)
269                 return (1);
270
271         ASSERT3P(m1, ==, m2);
272
273         return (0);
274 }
275
276 /*
277  * Update the allocatable flag and the metaslab group's capacity.
278  * The allocatable flag is set to true if the capacity is below
279  * the zfs_mg_noalloc_threshold. If a metaslab group transitions
280  * from allocatable to non-allocatable or vice versa then the metaslab
281  * group's class is updated to reflect the transition.
282  */
283 static void
284 metaslab_group_alloc_update(metaslab_group_t *mg)
285 {
286         vdev_t *vd = mg->mg_vd;
287         metaslab_class_t *mc = mg->mg_class;
288         vdev_stat_t *vs = &vd->vdev_stat;
289         boolean_t was_allocatable;
290
291         ASSERT(vd == vd->vdev_top);
292
293         mutex_enter(&mg->mg_lock);
294         was_allocatable = mg->mg_allocatable;
295
296         mg->mg_free_capacity = ((vs->vs_space - vs->vs_alloc) * 100) /
297             (vs->vs_space + 1);
298
299         mg->mg_allocatable = (mg->mg_free_capacity > zfs_mg_noalloc_threshold);
300
301         /*
302          * The mc_alloc_groups maintains a count of the number of
303          * groups in this metaslab class that are still above the
304          * zfs_mg_noalloc_threshold. This is used by the allocating
305          * threads to determine if they should avoid allocations to
306          * a given group. The allocator will avoid allocations to a group
307          * if that group has reached or is below the zfs_mg_noalloc_threshold
308          * and there are still other groups that are above the threshold.
309          * When a group transitions from allocatable to non-allocatable or
310          * vice versa we update the metaslab class to reflect that change.
311          * When the mc_alloc_groups value drops to 0 that means that all
312          * groups have reached the zfs_mg_noalloc_threshold making all groups
313          * eligible for allocations. This effectively means that all devices
314          * are balanced again.
315          */
316         if (was_allocatable && !mg->mg_allocatable)
317                 mc->mc_alloc_groups--;
318         else if (!was_allocatable && mg->mg_allocatable)
319                 mc->mc_alloc_groups++;
320         mutex_exit(&mg->mg_lock);
321 }
322
323 metaslab_group_t *
324 metaslab_group_create(metaslab_class_t *mc, vdev_t *vd)
325 {
326         metaslab_group_t *mg;
327
328         mg = kmem_zalloc(sizeof (metaslab_group_t), KM_PUSHPAGE);
329         mutex_init(&mg->mg_lock, NULL, MUTEX_DEFAULT, NULL);
330         avl_create(&mg->mg_metaslab_tree, metaslab_compare,
331             sizeof (metaslab_t), offsetof(struct metaslab, ms_group_node));
332         mg->mg_vd = vd;
333         mg->mg_class = mc;
334         mg->mg_activation_count = 0;
335
336         mg->mg_taskq = taskq_create("metaslab_group_taskq", metaslab_load_pct,
337             minclsyspri, 10, INT_MAX, TASKQ_THREADS_CPU_PCT);
338
339         return (mg);
340 }
341
342 void
343 metaslab_group_destroy(metaslab_group_t *mg)
344 {
345         ASSERT(mg->mg_prev == NULL);
346         ASSERT(mg->mg_next == NULL);
347         /*
348          * We may have gone below zero with the activation count
349          * either because we never activated in the first place or
350          * because we're done, and possibly removing the vdev.
351          */
352         ASSERT(mg->mg_activation_count <= 0);
353
354         taskq_destroy(mg->mg_taskq);
355         avl_destroy(&mg->mg_metaslab_tree);
356         mutex_destroy(&mg->mg_lock);
357         kmem_free(mg, sizeof (metaslab_group_t));
358 }
359
360 void
361 metaslab_group_activate(metaslab_group_t *mg)
362 {
363         metaslab_class_t *mc = mg->mg_class;
364         metaslab_group_t *mgprev, *mgnext;
365
366         ASSERT(spa_config_held(mc->mc_spa, SCL_ALLOC, RW_WRITER));
367
368         ASSERT(mc->mc_rotor != mg);
369         ASSERT(mg->mg_prev == NULL);
370         ASSERT(mg->mg_next == NULL);
371         ASSERT(mg->mg_activation_count <= 0);
372
373         if (++mg->mg_activation_count <= 0)
374                 return;
375
376         mg->mg_aliquot = metaslab_aliquot * MAX(1, mg->mg_vd->vdev_children);
377         metaslab_group_alloc_update(mg);
378
379         if ((mgprev = mc->mc_rotor) == NULL) {
380                 mg->mg_prev = mg;
381                 mg->mg_next = mg;
382         } else {
383                 mgnext = mgprev->mg_next;
384                 mg->mg_prev = mgprev;
385                 mg->mg_next = mgnext;
386                 mgprev->mg_next = mg;
387                 mgnext->mg_prev = mg;
388         }
389         mc->mc_rotor = mg;
390 }
391
392 void
393 metaslab_group_passivate(metaslab_group_t *mg)
394 {
395         metaslab_class_t *mc = mg->mg_class;
396         metaslab_group_t *mgprev, *mgnext;
397
398         ASSERT(spa_config_held(mc->mc_spa, SCL_ALLOC, RW_WRITER));
399
400         if (--mg->mg_activation_count != 0) {
401                 ASSERT(mc->mc_rotor != mg);
402                 ASSERT(mg->mg_prev == NULL);
403                 ASSERT(mg->mg_next == NULL);
404                 ASSERT(mg->mg_activation_count < 0);
405                 return;
406         }
407
408         taskq_wait(mg->mg_taskq);
409
410         mgprev = mg->mg_prev;
411         mgnext = mg->mg_next;
412
413         if (mg == mgnext) {
414                 mc->mc_rotor = NULL;
415         } else {
416                 mc->mc_rotor = mgnext;
417                 mgprev->mg_next = mgnext;
418                 mgnext->mg_prev = mgprev;
419         }
420
421         mg->mg_prev = NULL;
422         mg->mg_next = NULL;
423 }
424
425 static void
426 metaslab_group_add(metaslab_group_t *mg, metaslab_t *msp)
427 {
428         mutex_enter(&mg->mg_lock);
429         ASSERT(msp->ms_group == NULL);
430         msp->ms_group = mg;
431         msp->ms_weight = 0;
432         avl_add(&mg->mg_metaslab_tree, msp);
433         mutex_exit(&mg->mg_lock);
434 }
435
436 static void
437 metaslab_group_remove(metaslab_group_t *mg, metaslab_t *msp)
438 {
439         mutex_enter(&mg->mg_lock);
440         ASSERT(msp->ms_group == mg);
441         avl_remove(&mg->mg_metaslab_tree, msp);
442         msp->ms_group = NULL;
443         mutex_exit(&mg->mg_lock);
444 }
445
446 static void
447 metaslab_group_sort(metaslab_group_t *mg, metaslab_t *msp, uint64_t weight)
448 {
449         /*
450          * Although in principle the weight can be any value, in
451          * practice we do not use values in the range [1, 510].
452          */
453         ASSERT(weight >= SPA_MINBLOCKSIZE-1 || weight == 0);
454         ASSERT(MUTEX_HELD(&msp->ms_lock));
455
456         mutex_enter(&mg->mg_lock);
457         ASSERT(msp->ms_group == mg);
458         avl_remove(&mg->mg_metaslab_tree, msp);
459         msp->ms_weight = weight;
460         avl_add(&mg->mg_metaslab_tree, msp);
461         mutex_exit(&mg->mg_lock);
462 }
463
464 /*
465  * Determine if a given metaslab group should skip allocations. A metaslab
466  * group should avoid allocations if its used capacity has crossed the
467  * zfs_mg_noalloc_threshold and there is at least one metaslab group
468  * that can still handle allocations.
469  */
470 static boolean_t
471 metaslab_group_allocatable(metaslab_group_t *mg)
472 {
473         vdev_t *vd = mg->mg_vd;
474         spa_t *spa = vd->vdev_spa;
475         metaslab_class_t *mc = mg->mg_class;
476
477         /*
478          * A metaslab group is considered allocatable if its free capacity
479          * is greater than the set value of zfs_mg_noalloc_threshold, it's
480          * associated with a slog, or there are no other metaslab groups
481          * with free capacity greater than zfs_mg_noalloc_threshold.
482          */
483         return (mg->mg_free_capacity > zfs_mg_noalloc_threshold ||
484             mc != spa_normal_class(spa) || mc->mc_alloc_groups == 0);
485 }
486
487 /*
488  * ==========================================================================
489  * Range tree callbacks
490  * ==========================================================================
491  */
492
493 /*
494  * Comparison function for the private size-ordered tree. Tree is sorted
495  * by size, larger sizes at the end of the tree.
496  */
497 static int
498 metaslab_rangesize_compare(const void *x1, const void *x2)
499 {
500         const range_seg_t *r1 = x1;
501         const range_seg_t *r2 = x2;
502         uint64_t rs_size1 = r1->rs_end - r1->rs_start;
503         uint64_t rs_size2 = r2->rs_end - r2->rs_start;
504
505         if (rs_size1 < rs_size2)
506                 return (-1);
507         if (rs_size1 > rs_size2)
508                 return (1);
509
510         if (r1->rs_start < r2->rs_start)
511                 return (-1);
512
513         if (r1->rs_start > r2->rs_start)
514                 return (1);
515
516         return (0);
517 }
518
519 /*
520  * Create any block allocator specific components. The current allocators
521  * rely on using both a size-ordered range_tree_t and an array of uint64_t's.
522  */
523 static void
524 metaslab_rt_create(range_tree_t *rt, void *arg)
525 {
526         metaslab_t *msp = arg;
527
528         ASSERT3P(rt->rt_arg, ==, msp);
529         ASSERT(msp->ms_tree == NULL);
530
531         avl_create(&msp->ms_size_tree, metaslab_rangesize_compare,
532             sizeof (range_seg_t), offsetof(range_seg_t, rs_pp_node));
533 }
534
535 /*
536  * Destroy the block allocator specific components.
537  */
538 static void
539 metaslab_rt_destroy(range_tree_t *rt, void *arg)
540 {
541         metaslab_t *msp = arg;
542
543         ASSERT3P(rt->rt_arg, ==, msp);
544         ASSERT3P(msp->ms_tree, ==, rt);
545         ASSERT0(avl_numnodes(&msp->ms_size_tree));
546
547         avl_destroy(&msp->ms_size_tree);
548 }
549
550 static void
551 metaslab_rt_add(range_tree_t *rt, range_seg_t *rs, void *arg)
552 {
553         metaslab_t *msp = arg;
554
555         ASSERT3P(rt->rt_arg, ==, msp);
556         ASSERT3P(msp->ms_tree, ==, rt);
557         VERIFY(!msp->ms_condensing);
558         avl_add(&msp->ms_size_tree, rs);
559 }
560
561 static void
562 metaslab_rt_remove(range_tree_t *rt, range_seg_t *rs, void *arg)
563 {
564         metaslab_t *msp = arg;
565
566         ASSERT3P(rt->rt_arg, ==, msp);
567         ASSERT3P(msp->ms_tree, ==, rt);
568         VERIFY(!msp->ms_condensing);
569         avl_remove(&msp->ms_size_tree, rs);
570 }
571
572 static void
573 metaslab_rt_vacate(range_tree_t *rt, void *arg)
574 {
575         metaslab_t *msp = arg;
576
577         ASSERT3P(rt->rt_arg, ==, msp);
578         ASSERT3P(msp->ms_tree, ==, rt);
579
580         /*
581          * Normally one would walk the tree freeing nodes along the way.
582          * Since the nodes are shared with the range trees we can avoid
583          * walking all nodes and just reinitialize the avl tree. The nodes
584          * will be freed by the range tree, so we don't want to free them here.
585          */
586         avl_create(&msp->ms_size_tree, metaslab_rangesize_compare,
587             sizeof (range_seg_t), offsetof(range_seg_t, rs_pp_node));
588 }
589
590 static range_tree_ops_t metaslab_rt_ops = {
591         metaslab_rt_create,
592         metaslab_rt_destroy,
593         metaslab_rt_add,
594         metaslab_rt_remove,
595         metaslab_rt_vacate
596 };
597
598 /*
599  * ==========================================================================
600  * Metaslab block operations
601  * ==========================================================================
602  */
603
604 /*
605  * Return the maximum contiguous segment within the metaslab.
606  */
607 uint64_t
608 metaslab_block_maxsize(metaslab_t *msp)
609 {
610         avl_tree_t *t = &msp->ms_size_tree;
611         range_seg_t *rs;
612
613         if (t == NULL || (rs = avl_last(t)) == NULL)
614                 return (0ULL);
615
616         return (rs->rs_end - rs->rs_start);
617 }
618
619 uint64_t
620 metaslab_block_alloc(metaslab_t *msp, uint64_t size)
621 {
622         uint64_t start;
623         range_tree_t *rt = msp->ms_tree;
624
625         VERIFY(!msp->ms_condensing);
626
627         start = msp->ms_ops->msop_alloc(msp, size);
628         if (start != -1ULL) {
629                 vdev_t *vd = msp->ms_group->mg_vd;
630
631                 VERIFY0(P2PHASE(start, 1ULL << vd->vdev_ashift));
632                 VERIFY0(P2PHASE(size, 1ULL << vd->vdev_ashift));
633                 VERIFY3U(range_tree_space(rt) - size, <=, msp->ms_size);
634                 range_tree_remove(rt, start, size);
635         }
636         return (start);
637 }
638
639 /*
640  * ==========================================================================
641  * Common allocator routines
642  * ==========================================================================
643  */
644
645 #if defined(WITH_FF_BLOCK_ALLOCATOR) || \
646     defined(WITH_DF_BLOCK_ALLOCATOR) || \
647     defined(WITH_CF_BLOCK_ALLOCATOR)
648 /*
649  * This is a helper function that can be used by the allocator to find
650  * a suitable block to allocate. This will search the specified AVL
651  * tree looking for a block that matches the specified criteria.
652  */
653 static uint64_t
654 metaslab_block_picker(avl_tree_t *t, uint64_t *cursor, uint64_t size,
655     uint64_t align)
656 {
657         range_seg_t *rs, rsearch;
658         avl_index_t where;
659
660         rsearch.rs_start = *cursor;
661         rsearch.rs_end = *cursor + size;
662
663         rs = avl_find(t, &rsearch, &where);
664         if (rs == NULL)
665                 rs = avl_nearest(t, where, AVL_AFTER);
666
667         while (rs != NULL) {
668                 uint64_t offset = P2ROUNDUP(rs->rs_start, align);
669
670                 if (offset + size <= rs->rs_end) {
671                         *cursor = offset + size;
672                         return (offset);
673                 }
674                 rs = AVL_NEXT(t, rs);
675         }
676
677         /*
678          * If we know we've searched the whole map (*cursor == 0), give up.
679          * Otherwise, reset the cursor to the beginning and try again.
680          */
681         if (*cursor == 0)
682                 return (-1ULL);
683
684         *cursor = 0;
685         return (metaslab_block_picker(t, cursor, size, align));
686 }
687 #endif /* WITH_FF/DF/CF_BLOCK_ALLOCATOR */
688
689 #if defined(WITH_FF_BLOCK_ALLOCATOR)
690 /*
691  * ==========================================================================
692  * The first-fit block allocator
693  * ==========================================================================
694  */
695 static uint64_t
696 metaslab_ff_alloc(metaslab_t *msp, uint64_t size)
697 {
698         /*
699          * Find the largest power of 2 block size that evenly divides the
700          * requested size. This is used to try to allocate blocks with similar
701          * alignment from the same area of the metaslab (i.e. same cursor
702          * bucket) but it does not guarantee that other allocations sizes
703          * may exist in the same region.
704          */
705         uint64_t align = size & -size;
706         uint64_t *cursor = &msp->ms_lbas[highbit(align) - 1];
707         avl_tree_t *t = &msp->ms_tree->rt_root;
708
709         return (metaslab_block_picker(t, cursor, size, align));
710 }
711
712 /* ARGSUSED */
713 static boolean_t
714 metaslab_ff_fragmented(metaslab_t *msp)
715 {
716         return (B_TRUE);
717 }
718
719 static metaslab_ops_t metaslab_ff_ops = {
720         metaslab_ff_alloc,
721         metaslab_ff_fragmented
722 };
723
724 metaslab_ops_t *zfs_metaslab_ops = &metaslab_ff_ops;
725 #endif /* WITH_FF_BLOCK_ALLOCATOR */
726
727 #if defined(WITH_DF_BLOCK_ALLOCATOR)
728 /*
729  * ==========================================================================
730  * Dynamic block allocator -
731  * Uses the first fit allocation scheme until space get low and then
732  * adjusts to a best fit allocation method. Uses metaslab_df_alloc_threshold
733  * and metaslab_df_free_pct to determine when to switch the allocation scheme.
734  * ==========================================================================
735  */
736 static uint64_t
737 metaslab_df_alloc(metaslab_t *msp, uint64_t size)
738 {
739         /*
740          * Find the largest power of 2 block size that evenly divides the
741          * requested size. This is used to try to allocate blocks with similar
742          * alignment from the same area of the metaslab (i.e. same cursor
743          * bucket) but it does not guarantee that other allocations sizes
744          * may exist in the same region.
745          */
746         uint64_t align = size & -size;
747         uint64_t *cursor = &msp->ms_lbas[highbit(align) - 1];
748         range_tree_t *rt = msp->ms_tree;
749         avl_tree_t *t = &rt->rt_root;
750         uint64_t max_size = metaslab_block_maxsize(msp);
751         int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
752
753         ASSERT(MUTEX_HELD(&msp->ms_lock));
754         ASSERT3U(avl_numnodes(t), ==, avl_numnodes(&msp->ms_size_tree));
755
756         if (max_size < size)
757                 return (-1ULL);
758
759         /*
760          * If we're running low on space switch to using the size
761          * sorted AVL tree (best-fit).
762          */
763         if (max_size < metaslab_df_alloc_threshold ||
764             free_pct < metaslab_df_free_pct) {
765                 t = &msp->ms_size_tree;
766                 *cursor = 0;
767         }
768
769         return (metaslab_block_picker(t, cursor, size, 1ULL));
770 }
771
772 static boolean_t
773 metaslab_df_fragmented(metaslab_t *msp)
774 {
775         range_tree_t *rt = msp->ms_tree;
776         uint64_t max_size = metaslab_block_maxsize(msp);
777         int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
778
779         if (max_size >= metaslab_df_alloc_threshold &&
780             free_pct >= metaslab_df_free_pct)
781                 return (B_FALSE);
782
783
784         return (B_TRUE);
785 }
786
787 static metaslab_ops_t metaslab_df_ops = {
788         metaslab_df_alloc,
789         metaslab_df_fragmented
790 };
791
792 metaslab_ops_t *zfs_metaslab_ops = &metaslab_df_ops;
793 #endif /* WITH_DF_BLOCK_ALLOCATOR */
794
795 #if defined(WITH_CF_BLOCK_ALLOCATOR)
796 /*
797  * ==========================================================================
798  * Cursor fit block allocator -
799  * Select the largest region in the metaslab, set the cursor to the beginning
800  * of the range and the cursor_end to the end of the range. As allocations
801  * are made advance the cursor. Continue allocating from the cursor until
802  * the range is exhausted and then find a new range.
803  * ==========================================================================
804  */
805 static uint64_t
806 metaslab_cf_alloc(metaslab_t *msp, uint64_t size)
807 {
808         range_tree_t *rt = msp->ms_tree;
809         avl_tree_t *t = &msp->ms_size_tree;
810         uint64_t *cursor = &msp->ms_lbas[0];
811         uint64_t *cursor_end = &msp->ms_lbas[1];
812         uint64_t offset = 0;
813
814         ASSERT(MUTEX_HELD(&msp->ms_lock));
815         ASSERT3U(avl_numnodes(t), ==, avl_numnodes(&rt->rt_root));
816
817         ASSERT3U(*cursor_end, >=, *cursor);
818
819         if ((*cursor + size) > *cursor_end) {
820                 range_seg_t *rs;
821
822                 rs = avl_last(&msp->ms_size_tree);
823                 if (rs == NULL || (rs->rs_end - rs->rs_start) < size)
824                         return (-1ULL);
825
826                 *cursor = rs->rs_start;
827                 *cursor_end = rs->rs_end;
828         }
829
830         offset = *cursor;
831         *cursor += size;
832
833         return (offset);
834 }
835
836 static boolean_t
837 metaslab_cf_fragmented(metaslab_t *msp)
838 {
839         return (metaslab_block_maxsize(msp) < metaslab_min_alloc_size);
840 }
841
842 static metaslab_ops_t metaslab_cf_ops = {
843         metaslab_cf_alloc,
844         metaslab_cf_fragmented
845 };
846
847 metaslab_ops_t *zfs_metaslab_ops = &metaslab_cf_ops;
848 #endif /* WITH_CF_BLOCK_ALLOCATOR */
849
850 #if defined(WITH_NDF_BLOCK_ALLOCATOR)
851 /*
852  * ==========================================================================
853  * New dynamic fit allocator -
854  * Select a region that is large enough to allocate 2^metaslab_ndf_clump_shift
855  * contiguous blocks. If no region is found then just use the largest segment
856  * that remains.
857  * ==========================================================================
858  */
859
860 /*
861  * Determines desired number of contiguous blocks (2^metaslab_ndf_clump_shift)
862  * to request from the allocator.
863  */
864 uint64_t metaslab_ndf_clump_shift = 4;
865
866 static uint64_t
867 metaslab_ndf_alloc(metaslab_t *msp, uint64_t size)
868 {
869         avl_tree_t *t = &msp->ms_tree->rt_root;
870         avl_index_t where;
871         range_seg_t *rs, rsearch;
872         uint64_t hbit = highbit(size);
873         uint64_t *cursor = &msp->ms_lbas[hbit - 1];
874         uint64_t max_size = metaslab_block_maxsize(msp);
875
876         ASSERT(MUTEX_HELD(&msp->ms_lock));
877         ASSERT3U(avl_numnodes(t), ==, avl_numnodes(&msp->ms_size_tree));
878
879         if (max_size < size)
880                 return (-1ULL);
881
882         rsearch.rs_start = *cursor;
883         rsearch.rs_end = *cursor + size;
884
885         rs = avl_find(t, &rsearch, &where);
886         if (rs == NULL || (rs->rs_end - rs->rs_start) < size) {
887                 t = &msp->ms_size_tree;
888
889                 rsearch.rs_start = 0;
890                 rsearch.rs_end = MIN(max_size,
891                     1ULL << (hbit + metaslab_ndf_clump_shift));
892                 rs = avl_find(t, &rsearch, &where);
893                 if (rs == NULL)
894                         rs = avl_nearest(t, where, AVL_AFTER);
895                 ASSERT(rs != NULL);
896         }
897
898         if ((rs->rs_end - rs->rs_start) >= size) {
899                 *cursor = rs->rs_start + size;
900                 return (rs->rs_start);
901         }
902         return (-1ULL);
903 }
904
905 static boolean_t
906 metaslab_ndf_fragmented(metaslab_t *msp)
907 {
908         return (metaslab_block_maxsize(msp) <=
909             (metaslab_min_alloc_size << metaslab_ndf_clump_shift));
910 }
911
912 static metaslab_ops_t metaslab_ndf_ops = {
913         metaslab_ndf_alloc,
914         metaslab_ndf_fragmented
915 };
916
917 metaslab_ops_t *zfs_metaslab_ops = &metaslab_ndf_ops;
918 #endif /* WITH_NDF_BLOCK_ALLOCATOR */
919
920
921 /*
922  * ==========================================================================
923  * Metaslabs
924  * ==========================================================================
925  */
926
927 /*
928  * Wait for any in-progress metaslab loads to complete.
929  */
930 void
931 metaslab_load_wait(metaslab_t *msp)
932 {
933         ASSERT(MUTEX_HELD(&msp->ms_lock));
934
935         while (msp->ms_loading) {
936                 ASSERT(!msp->ms_loaded);
937                 cv_wait(&msp->ms_load_cv, &msp->ms_lock);
938         }
939 }
940
941 int
942 metaslab_load(metaslab_t *msp)
943 {
944         int error = 0;
945         int t;
946
947         ASSERT(MUTEX_HELD(&msp->ms_lock));
948         ASSERT(!msp->ms_loaded);
949         ASSERT(!msp->ms_loading);
950
951         msp->ms_loading = B_TRUE;
952
953         /*
954          * If the space map has not been allocated yet, then treat
955          * all the space in the metaslab as free and add it to the
956          * ms_tree.
957          */
958         if (msp->ms_sm != NULL)
959                 error = space_map_load(msp->ms_sm, msp->ms_tree, SM_FREE);
960         else
961                 range_tree_add(msp->ms_tree, msp->ms_start, msp->ms_size);
962
963         msp->ms_loaded = (error == 0);
964         msp->ms_loading = B_FALSE;
965
966         if (msp->ms_loaded) {
967                 for (t = 0; t < TXG_DEFER_SIZE; t++) {
968                         range_tree_walk(msp->ms_defertree[t],
969                             range_tree_remove, msp->ms_tree);
970                 }
971         }
972         cv_broadcast(&msp->ms_load_cv);
973         return (error);
974 }
975
976 void
977 metaslab_unload(metaslab_t *msp)
978 {
979         ASSERT(MUTEX_HELD(&msp->ms_lock));
980         range_tree_vacate(msp->ms_tree, NULL, NULL);
981         msp->ms_loaded = B_FALSE;
982         msp->ms_weight &= ~METASLAB_ACTIVE_MASK;
983 }
984
985 metaslab_t *
986 metaslab_init(metaslab_group_t *mg, uint64_t id, uint64_t object, uint64_t txg)
987 {
988         vdev_t *vd = mg->mg_vd;
989         objset_t *mos = vd->vdev_spa->spa_meta_objset;
990         metaslab_t *msp;
991
992         msp = kmem_zalloc(sizeof (metaslab_t), KM_PUSHPAGE);
993         mutex_init(&msp->ms_lock, NULL, MUTEX_DEFAULT, NULL);
994         cv_init(&msp->ms_load_cv, NULL, CV_DEFAULT, NULL);
995         msp->ms_id = id;
996         msp->ms_start = id << vd->vdev_ms_shift;
997         msp->ms_size = 1ULL << vd->vdev_ms_shift;
998
999         /*
1000          * We only open space map objects that already exist. All others
1001          * will be opened when we finally allocate an object for it.
1002          */
1003         if (object != 0) {
1004                 VERIFY0(space_map_open(&msp->ms_sm, mos, object, msp->ms_start,
1005                     msp->ms_size, vd->vdev_ashift, &msp->ms_lock));
1006                 ASSERT(msp->ms_sm != NULL);
1007         }
1008
1009         /*
1010          * We create the main range tree here, but we don't create the
1011          * alloctree and freetree until metaslab_sync_done().  This serves
1012          * two purposes: it allows metaslab_sync_done() to detect the
1013          * addition of new space; and for debugging, it ensures that we'd
1014          * data fault on any attempt to use this metaslab before it's ready.
1015          */
1016         msp->ms_tree = range_tree_create(&metaslab_rt_ops, msp, &msp->ms_lock);
1017         metaslab_group_add(mg, msp);
1018
1019         msp->ms_ops = mg->mg_class->mc_ops;
1020
1021         /*
1022          * If we're opening an existing pool (txg == 0) or creating
1023          * a new one (txg == TXG_INITIAL), all space is available now.
1024          * If we're adding space to an existing pool, the new space
1025          * does not become available until after this txg has synced.
1026          */
1027         if (txg <= TXG_INITIAL)
1028                 metaslab_sync_done(msp, 0);
1029
1030         /*
1031          * If metaslab_debug_load is set and we're initializing a metaslab
1032          * that has an allocated space_map object then load the its space
1033          * map so that can verify frees.
1034          */
1035         if (metaslab_debug_load && msp->ms_sm != NULL) {
1036                 mutex_enter(&msp->ms_lock);
1037                 VERIFY0(metaslab_load(msp));
1038                 mutex_exit(&msp->ms_lock);
1039         }
1040
1041         if (txg != 0) {
1042                 vdev_dirty(vd, 0, NULL, txg);
1043                 vdev_dirty(vd, VDD_METASLAB, msp, txg);
1044         }
1045
1046         return (msp);
1047 }
1048
1049 void
1050 metaslab_fini(metaslab_t *msp)
1051 {
1052         int t;
1053
1054         metaslab_group_t *mg = msp->ms_group;
1055
1056         metaslab_group_remove(mg, msp);
1057
1058         mutex_enter(&msp->ms_lock);
1059
1060         VERIFY(msp->ms_group == NULL);
1061         vdev_space_update(mg->mg_vd, -space_map_allocated(msp->ms_sm),
1062             0, -msp->ms_size);
1063         space_map_close(msp->ms_sm);
1064
1065         metaslab_unload(msp);
1066         range_tree_destroy(msp->ms_tree);
1067
1068         for (t = 0; t < TXG_SIZE; t++) {
1069                 range_tree_destroy(msp->ms_alloctree[t]);
1070                 range_tree_destroy(msp->ms_freetree[t]);
1071         }
1072
1073         for (t = 0; t < TXG_DEFER_SIZE; t++) {
1074                 range_tree_destroy(msp->ms_defertree[t]);
1075         }
1076
1077         ASSERT0(msp->ms_deferspace);
1078
1079         mutex_exit(&msp->ms_lock);
1080         cv_destroy(&msp->ms_load_cv);
1081         mutex_destroy(&msp->ms_lock);
1082
1083         kmem_free(msp, sizeof (metaslab_t));
1084 }
1085
1086 /*
1087  * Apply a weighting factor based on the histogram information for this
1088  * metaslab. The current weighting factor is somewhat arbitrary and requires
1089  * additional investigation. The implementation provides a measure of
1090  * "weighted" free space and gives a higher weighting for larger contiguous
1091  * regions. The weighting factor is determined by counting the number of
1092  * sm_shift sectors that exist in each region represented by the histogram.
1093  * That value is then multiplied by the power of 2 exponent and the sm_shift
1094  * value.
1095  *
1096  * For example, assume the 2^21 histogram bucket has 4 2MB regions and the
1097  * metaslab has an sm_shift value of 9 (512B):
1098  *
1099  * 1) calculate the number of sm_shift sectors in the region:
1100  *      2^21 / 2^9 = 2^12 = 4096 * 4 (number of regions) = 16384
1101  * 2) multiply by the power of 2 exponent and the sm_shift value:
1102  *      16384 * 21 * 9 = 3096576
1103  * This value will be added to the weighting of the metaslab.
1104  */
1105 static uint64_t
1106 metaslab_weight_factor(metaslab_t *msp)
1107 {
1108         uint64_t factor = 0;
1109         uint64_t sectors;
1110         int i;
1111
1112         /*
1113          * A null space map means that the entire metaslab is free,
1114          * calculate a weight factor that spans the entire size of the
1115          * metaslab.
1116          */
1117         if (msp->ms_sm == NULL) {
1118                 vdev_t *vd = msp->ms_group->mg_vd;
1119
1120                 i = highbit(msp->ms_size) - 1;
1121                 sectors = msp->ms_size >> vd->vdev_ashift;
1122                 return (sectors * i * vd->vdev_ashift);
1123         }
1124
1125         if (msp->ms_sm->sm_dbuf->db_size != sizeof (space_map_phys_t))
1126                 return (0);
1127
1128         for (i = 0; i < SPACE_MAP_HISTOGRAM_SIZE(msp->ms_sm); i++) {
1129                 if (msp->ms_sm->sm_phys->smp_histogram[i] == 0)
1130                         continue;
1131
1132                 /*
1133                  * Determine the number of sm_shift sectors in the region
1134                  * indicated by the histogram. For example, given an
1135                  * sm_shift value of 9 (512 bytes) and i = 4 then we know
1136                  * that we're looking at an 8K region in the histogram
1137                  * (i.e. 9 + 4 = 13, 2^13 = 8192). To figure out the
1138                  * number of sm_shift sectors (512 bytes in this example),
1139                  * we would take 8192 / 512 = 16. Since the histogram
1140                  * is offset by sm_shift we can simply use the value of
1141                  * of i to calculate this (i.e. 2^i = 16 where i = 4).
1142                  */
1143                 sectors = msp->ms_sm->sm_phys->smp_histogram[i] << i;
1144                 factor += (i + msp->ms_sm->sm_shift) * sectors;
1145         }
1146         return (factor * msp->ms_sm->sm_shift);
1147 }
1148
1149 static uint64_t
1150 metaslab_weight(metaslab_t *msp)
1151 {
1152         metaslab_group_t *mg = msp->ms_group;
1153         vdev_t *vd = mg->mg_vd;
1154         uint64_t weight, space;
1155
1156         ASSERT(MUTEX_HELD(&msp->ms_lock));
1157
1158         /*
1159          * This vdev is in the process of being removed so there is nothing
1160          * for us to do here.
1161          */
1162         if (vd->vdev_removing) {
1163                 ASSERT0(space_map_allocated(msp->ms_sm));
1164                 ASSERT0(vd->vdev_ms_shift);
1165                 return (0);
1166         }
1167
1168         /*
1169          * The baseline weight is the metaslab's free space.
1170          */
1171         space = msp->ms_size - space_map_allocated(msp->ms_sm);
1172         weight = space;
1173
1174         /*
1175          * Modern disks have uniform bit density and constant angular velocity.
1176          * Therefore, the outer recording zones are faster (higher bandwidth)
1177          * than the inner zones by the ratio of outer to inner track diameter,
1178          * which is typically around 2:1.  We account for this by assigning
1179          * higher weight to lower metaslabs (multiplier ranging from 2x to 1x).
1180          * In effect, this means that we'll select the metaslab with the most
1181          * free bandwidth rather than simply the one with the most free space.
1182          */
1183         weight = 2 * weight - (msp->ms_id * weight) / vd->vdev_ms_count;
1184         ASSERT(weight >= space && weight <= 2 * space);
1185
1186         msp->ms_factor = metaslab_weight_factor(msp);
1187         if (metaslab_weight_factor_enable)
1188                 weight += msp->ms_factor;
1189
1190         if (msp->ms_loaded && !msp->ms_ops->msop_fragmented(msp)) {
1191                 /*
1192                  * If this metaslab is one we're actively using, adjust its
1193                  * weight to make it preferable to any inactive metaslab so
1194                  * we'll polish it off.
1195                  */
1196                 weight |= (msp->ms_weight & METASLAB_ACTIVE_MASK);
1197         }
1198
1199         return (weight);
1200 }
1201
1202 static int
1203 metaslab_activate(metaslab_t *msp, uint64_t activation_weight)
1204 {
1205         ASSERT(MUTEX_HELD(&msp->ms_lock));
1206
1207         if ((msp->ms_weight & METASLAB_ACTIVE_MASK) == 0) {
1208                 metaslab_load_wait(msp);
1209                 if (!msp->ms_loaded) {
1210                         int error = metaslab_load(msp);
1211                         if (error) {
1212                                 metaslab_group_sort(msp->ms_group, msp, 0);
1213                                 return (error);
1214                         }
1215                 }
1216
1217                 metaslab_group_sort(msp->ms_group, msp,
1218                     msp->ms_weight | activation_weight);
1219         }
1220         ASSERT(msp->ms_loaded);
1221         ASSERT(msp->ms_weight & METASLAB_ACTIVE_MASK);
1222
1223         return (0);
1224 }
1225
1226 static void
1227 metaslab_passivate(metaslab_t *msp, uint64_t size)
1228 {
1229         /*
1230          * If size < SPA_MINBLOCKSIZE, then we will not allocate from
1231          * this metaslab again.  In that case, it had better be empty,
1232          * or we would be leaving space on the table.
1233          */
1234         ASSERT(size >= SPA_MINBLOCKSIZE || range_tree_space(msp->ms_tree) == 0);
1235         metaslab_group_sort(msp->ms_group, msp, MIN(msp->ms_weight, size));
1236         ASSERT((msp->ms_weight & METASLAB_ACTIVE_MASK) == 0);
1237 }
1238
1239 static void
1240 metaslab_preload(void *arg)
1241 {
1242         metaslab_t *msp = arg;
1243         spa_t *spa = msp->ms_group->mg_vd->vdev_spa;
1244
1245         mutex_enter(&msp->ms_lock);
1246         metaslab_load_wait(msp);
1247         if (!msp->ms_loaded)
1248                 (void) metaslab_load(msp);
1249
1250         /*
1251          * Set the ms_access_txg value so that we don't unload it right away.
1252          */
1253         msp->ms_access_txg = spa_syncing_txg(spa) + metaslab_unload_delay + 1;
1254         mutex_exit(&msp->ms_lock);
1255 }
1256
1257 static void
1258 metaslab_group_preload(metaslab_group_t *mg)
1259 {
1260         spa_t *spa = mg->mg_vd->vdev_spa;
1261         metaslab_t *msp;
1262         avl_tree_t *t = &mg->mg_metaslab_tree;
1263         int m = 0;
1264
1265         if (spa_shutting_down(spa) || !metaslab_preload_enabled) {
1266                 taskq_wait(mg->mg_taskq);
1267                 return;
1268         }
1269         mutex_enter(&mg->mg_lock);
1270
1271         /*
1272          * Prefetch the next potential metaslabs
1273          */
1274         for (msp = avl_first(t); msp != NULL; msp = AVL_NEXT(t, msp)) {
1275
1276                 /* If we have reached our preload limit then we're done */
1277                 if (++m > metaslab_preload_limit)
1278                         break;
1279
1280                 VERIFY(taskq_dispatch(mg->mg_taskq, metaslab_preload,
1281                     msp, TQ_PUSHPAGE) != 0);
1282         }
1283         mutex_exit(&mg->mg_lock);
1284 }
1285
1286 /*
1287  * Determine if the space map's on-disk footprint is past our tolerance
1288  * for inefficiency. We would like to use the following criteria to make
1289  * our decision:
1290  *
1291  * 1. The size of the space map object should not dramatically increase as a
1292  * result of writing out the free space range tree.
1293  *
1294  * 2. The minimal on-disk space map representation is zfs_condense_pct/100
1295  * times the size than the free space range tree representation
1296  * (i.e. zfs_condense_pct = 110 and in-core = 1MB, minimal = 1.1.MB).
1297  *
1298  * Checking the first condition is tricky since we don't want to walk
1299  * the entire AVL tree calculating the estimated on-disk size. Instead we
1300  * use the size-ordered range tree in the metaslab and calculate the
1301  * size required to write out the largest segment in our free tree. If the
1302  * size required to represent that segment on disk is larger than the space
1303  * map object then we avoid condensing this map.
1304  *
1305  * To determine the second criterion we use a best-case estimate and assume
1306  * each segment can be represented on-disk as a single 64-bit entry. We refer
1307  * to this best-case estimate as the space map's minimal form.
1308  */
1309 static boolean_t
1310 metaslab_should_condense(metaslab_t *msp)
1311 {
1312         space_map_t *sm = msp->ms_sm;
1313         range_seg_t *rs;
1314         uint64_t size, entries, segsz;
1315
1316         ASSERT(MUTEX_HELD(&msp->ms_lock));
1317         ASSERT(msp->ms_loaded);
1318
1319         /*
1320          * Use the ms_size_tree range tree, which is ordered by size, to
1321          * obtain the largest segment in the free tree. If the tree is empty
1322          * then we should condense the map.
1323          */
1324         rs = avl_last(&msp->ms_size_tree);
1325         if (rs == NULL)
1326                 return (B_TRUE);
1327
1328         /*
1329          * Calculate the number of 64-bit entries this segment would
1330          * require when written to disk. If this single segment would be
1331          * larger on-disk than the entire current on-disk structure, then
1332          * clearly condensing will increase the on-disk structure size.
1333          */
1334         size = (rs->rs_end - rs->rs_start) >> sm->sm_shift;
1335         entries = size / (MIN(size, SM_RUN_MAX));
1336         segsz = entries * sizeof (uint64_t);
1337
1338         return (segsz <= space_map_length(msp->ms_sm) &&
1339             space_map_length(msp->ms_sm) >= (zfs_condense_pct *
1340             sizeof (uint64_t) * avl_numnodes(&msp->ms_tree->rt_root)) / 100);
1341 }
1342
1343 /*
1344  * Condense the on-disk space map representation to its minimized form.
1345  * The minimized form consists of a small number of allocations followed by
1346  * the entries of the free range tree.
1347  */
1348 static void
1349 metaslab_condense(metaslab_t *msp, uint64_t txg, dmu_tx_t *tx)
1350 {
1351         spa_t *spa = msp->ms_group->mg_vd->vdev_spa;
1352         range_tree_t *freetree = msp->ms_freetree[txg & TXG_MASK];
1353         range_tree_t *condense_tree;
1354         space_map_t *sm = msp->ms_sm;
1355         int t;
1356
1357         ASSERT(MUTEX_HELD(&msp->ms_lock));
1358         ASSERT3U(spa_sync_pass(spa), ==, 1);
1359         ASSERT(msp->ms_loaded);
1360
1361         spa_dbgmsg(spa, "condensing: txg %llu, msp[%llu] %p, "
1362             "smp size %llu, segments %lu", txg, msp->ms_id, msp,
1363             space_map_length(msp->ms_sm), avl_numnodes(&msp->ms_tree->rt_root));
1364
1365         /*
1366          * Create an range tree that is 100% allocated. We remove segments
1367          * that have been freed in this txg, any deferred frees that exist,
1368          * and any allocation in the future. Removing segments should be
1369          * a relatively inexpensive operation since we expect these trees to
1370          * have a small number of nodes.
1371          */
1372         condense_tree = range_tree_create(NULL, NULL, &msp->ms_lock);
1373         range_tree_add(condense_tree, msp->ms_start, msp->ms_size);
1374
1375         /*
1376          * Remove what's been freed in this txg from the condense_tree.
1377          * Since we're in sync_pass 1, we know that all the frees from
1378          * this txg are in the freetree.
1379          */
1380         range_tree_walk(freetree, range_tree_remove, condense_tree);
1381
1382         for (t = 0; t < TXG_DEFER_SIZE; t++) {
1383                 range_tree_walk(msp->ms_defertree[t],
1384                     range_tree_remove, condense_tree);
1385         }
1386
1387         for (t = 1; t < TXG_CONCURRENT_STATES; t++) {
1388                 range_tree_walk(msp->ms_alloctree[(txg + t) & TXG_MASK],
1389                     range_tree_remove, condense_tree);
1390         }
1391
1392         /*
1393          * We're about to drop the metaslab's lock thus allowing
1394          * other consumers to change it's content. Set the
1395          * metaslab's ms_condensing flag to ensure that
1396          * allocations on this metaslab do not occur while we're
1397          * in the middle of committing it to disk. This is only critical
1398          * for the ms_tree as all other range trees use per txg
1399          * views of their content.
1400          */
1401         msp->ms_condensing = B_TRUE;
1402
1403         mutex_exit(&msp->ms_lock);
1404         space_map_truncate(sm, tx);
1405         mutex_enter(&msp->ms_lock);
1406
1407         /*
1408          * While we would ideally like to create a space_map representation
1409          * that consists only of allocation records, doing so can be
1410          * prohibitively expensive because the in-core free tree can be
1411          * large, and therefore computationally expensive to subtract
1412          * from the condense_tree. Instead we sync out two trees, a cheap
1413          * allocation only tree followed by the in-core free tree. While not
1414          * optimal, this is typically close to optimal, and much cheaper to
1415          * compute.
1416          */
1417         space_map_write(sm, condense_tree, SM_ALLOC, tx);
1418         range_tree_vacate(condense_tree, NULL, NULL);
1419         range_tree_destroy(condense_tree);
1420
1421         space_map_write(sm, msp->ms_tree, SM_FREE, tx);
1422         msp->ms_condensing = B_FALSE;
1423 }
1424
1425 /*
1426  * Write a metaslab to disk in the context of the specified transaction group.
1427  */
1428 void
1429 metaslab_sync(metaslab_t *msp, uint64_t txg)
1430 {
1431         metaslab_group_t *mg = msp->ms_group;
1432         vdev_t *vd = mg->mg_vd;
1433         spa_t *spa = vd->vdev_spa;
1434         objset_t *mos = spa_meta_objset(spa);
1435         range_tree_t *alloctree = msp->ms_alloctree[txg & TXG_MASK];
1436         range_tree_t **freetree = &msp->ms_freetree[txg & TXG_MASK];
1437         range_tree_t **freed_tree =
1438             &msp->ms_freetree[TXG_CLEAN(txg) & TXG_MASK];
1439         dmu_tx_t *tx;
1440         uint64_t object = space_map_object(msp->ms_sm);
1441
1442         ASSERT(!vd->vdev_ishole);
1443
1444         /*
1445          * This metaslab has just been added so there's no work to do now.
1446          */
1447         if (*freetree == NULL) {
1448                 ASSERT3P(alloctree, ==, NULL);
1449                 return;
1450         }
1451
1452         ASSERT3P(alloctree, !=, NULL);
1453         ASSERT3P(*freetree, !=, NULL);
1454         ASSERT3P(*freed_tree, !=, NULL);
1455
1456         if (range_tree_space(alloctree) == 0 &&
1457             range_tree_space(*freetree) == 0)
1458                 return;
1459
1460         /*
1461          * The only state that can actually be changing concurrently with
1462          * metaslab_sync() is the metaslab's ms_tree.  No other thread can
1463          * be modifying this txg's alloctree, freetree, freed_tree, or
1464          * space_map_phys_t. Therefore, we only hold ms_lock to satify
1465          * space_map ASSERTs. We drop it whenever we call into the DMU,
1466          * because the DMU can call down to us (e.g. via zio_free()) at
1467          * any time.
1468          */
1469
1470         tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
1471
1472         if (msp->ms_sm == NULL) {
1473                 uint64_t new_object;
1474
1475                 new_object = space_map_alloc(mos, tx);
1476                 VERIFY3U(new_object, !=, 0);
1477
1478                 VERIFY0(space_map_open(&msp->ms_sm, mos, new_object,
1479                     msp->ms_start, msp->ms_size, vd->vdev_ashift,
1480                     &msp->ms_lock));
1481                 ASSERT(msp->ms_sm != NULL);
1482         }
1483
1484         mutex_enter(&msp->ms_lock);
1485
1486         if (msp->ms_loaded && spa_sync_pass(spa) == 1 &&
1487             metaslab_should_condense(msp)) {
1488                 metaslab_condense(msp, txg, tx);
1489         } else {
1490                 space_map_write(msp->ms_sm, alloctree, SM_ALLOC, tx);
1491                 space_map_write(msp->ms_sm, *freetree, SM_FREE, tx);
1492         }
1493
1494         range_tree_vacate(alloctree, NULL, NULL);
1495
1496         if (msp->ms_loaded) {
1497                 /*
1498                  * When the space map is loaded, we have an accruate
1499                  * histogram in the range tree. This gives us an opportunity
1500                  * to bring the space map's histogram up-to-date so we clear
1501                  * it first before updating it.
1502                  */
1503                 space_map_histogram_clear(msp->ms_sm);
1504                 space_map_histogram_add(msp->ms_sm, msp->ms_tree, tx);
1505         } else {
1506                 /*
1507                  * Since the space map is not loaded we simply update the
1508                  * exisiting histogram with what was freed in this txg. This
1509                  * means that the on-disk histogram may not have an accurate
1510                  * view of the free space but it's close enough to allow
1511                  * us to make allocation decisions.
1512                  */
1513                 space_map_histogram_add(msp->ms_sm, *freetree, tx);
1514         }
1515
1516         /*
1517          * For sync pass 1, we avoid traversing this txg's free range tree
1518          * and instead will just swap the pointers for freetree and
1519          * freed_tree. We can safely do this since the freed_tree is
1520          * guaranteed to be empty on the initial pass.
1521          */
1522         if (spa_sync_pass(spa) == 1) {
1523                 range_tree_swap(freetree, freed_tree);
1524         } else {
1525                 range_tree_vacate(*freetree, range_tree_add, *freed_tree);
1526         }
1527
1528         ASSERT0(range_tree_space(msp->ms_alloctree[txg & TXG_MASK]));
1529         ASSERT0(range_tree_space(msp->ms_freetree[txg & TXG_MASK]));
1530
1531         mutex_exit(&msp->ms_lock);
1532
1533         if (object != space_map_object(msp->ms_sm)) {
1534                 object = space_map_object(msp->ms_sm);
1535                 dmu_write(mos, vd->vdev_ms_array, sizeof (uint64_t) *
1536                     msp->ms_id, sizeof (uint64_t), &object, tx);
1537         }
1538         dmu_tx_commit(tx);
1539 }
1540
1541 /*
1542  * Called after a transaction group has completely synced to mark
1543  * all of the metaslab's free space as usable.
1544  */
1545 void
1546 metaslab_sync_done(metaslab_t *msp, uint64_t txg)
1547 {
1548         metaslab_group_t *mg = msp->ms_group;
1549         vdev_t *vd = mg->mg_vd;
1550         range_tree_t **freed_tree;
1551         range_tree_t **defer_tree;
1552         int64_t alloc_delta, defer_delta;
1553         int t;
1554
1555         ASSERT(!vd->vdev_ishole);
1556
1557         mutex_enter(&msp->ms_lock);
1558
1559         /*
1560          * If this metaslab is just becoming available, initialize its
1561          * alloctrees, freetrees, and defertree and add its capacity to
1562          * the vdev.
1563          */
1564         if (msp->ms_freetree[TXG_CLEAN(txg) & TXG_MASK] == NULL) {
1565                 for (t = 0; t < TXG_SIZE; t++) {
1566                         ASSERT(msp->ms_alloctree[t] == NULL);
1567                         ASSERT(msp->ms_freetree[t] == NULL);
1568
1569                         msp->ms_alloctree[t] = range_tree_create(NULL, msp,
1570                             &msp->ms_lock);
1571                         msp->ms_freetree[t] = range_tree_create(NULL, msp,
1572                             &msp->ms_lock);
1573                 }
1574
1575                 for (t = 0; t < TXG_DEFER_SIZE; t++) {
1576                         ASSERT(msp->ms_defertree[t] == NULL);
1577
1578                         msp->ms_defertree[t] = range_tree_create(NULL, msp,
1579                             &msp->ms_lock);
1580                 }
1581
1582                 vdev_space_update(vd, 0, 0, msp->ms_size);
1583         }
1584
1585         freed_tree = &msp->ms_freetree[TXG_CLEAN(txg) & TXG_MASK];
1586         defer_tree = &msp->ms_defertree[txg % TXG_DEFER_SIZE];
1587
1588         alloc_delta = space_map_alloc_delta(msp->ms_sm);
1589         defer_delta = range_tree_space(*freed_tree) -
1590             range_tree_space(*defer_tree);
1591
1592         vdev_space_update(vd, alloc_delta + defer_delta, defer_delta, 0);
1593
1594         ASSERT0(range_tree_space(msp->ms_alloctree[txg & TXG_MASK]));
1595         ASSERT0(range_tree_space(msp->ms_freetree[txg & TXG_MASK]));
1596
1597         /*
1598          * If there's a metaslab_load() in progress, wait for it to complete
1599          * so that we have a consistent view of the in-core space map.
1600          */
1601         metaslab_load_wait(msp);
1602
1603         /*
1604          * Move the frees from the defer_tree back to the free
1605          * range tree (if it's loaded). Swap the freed_tree and the
1606          * defer_tree -- this is safe to do because we've just emptied out
1607          * the defer_tree.
1608          */
1609         range_tree_vacate(*defer_tree,
1610             msp->ms_loaded ? range_tree_add : NULL, msp->ms_tree);
1611         range_tree_swap(freed_tree, defer_tree);
1612
1613         space_map_update(msp->ms_sm);
1614
1615         msp->ms_deferspace += defer_delta;
1616         ASSERT3S(msp->ms_deferspace, >=, 0);
1617         ASSERT3S(msp->ms_deferspace, <=, msp->ms_size);
1618         if (msp->ms_deferspace != 0) {
1619                 /*
1620                  * Keep syncing this metaslab until all deferred frees
1621                  * are back in circulation.
1622                  */
1623                 vdev_dirty(vd, VDD_METASLAB, msp, txg + 1);
1624         }
1625
1626         if (msp->ms_loaded && msp->ms_access_txg < txg) {
1627                 for (t = 1; t < TXG_CONCURRENT_STATES; t++) {
1628                         VERIFY0(range_tree_space(
1629                             msp->ms_alloctree[(txg + t) & TXG_MASK]));
1630                 }
1631
1632                 if (!metaslab_debug_unload)
1633                         metaslab_unload(msp);
1634         }
1635
1636         metaslab_group_sort(mg, msp, metaslab_weight(msp));
1637         mutex_exit(&msp->ms_lock);
1638
1639 }
1640
1641 void
1642 metaslab_sync_reassess(metaslab_group_t *mg)
1643 {
1644         int64_t failures = mg->mg_alloc_failures;
1645
1646         metaslab_group_alloc_update(mg);
1647         atomic_add_64(&mg->mg_alloc_failures, -failures);
1648
1649         /*
1650          * Preload the next potential metaslabs
1651          */
1652         metaslab_group_preload(mg);
1653 }
1654
1655 static uint64_t
1656 metaslab_distance(metaslab_t *msp, dva_t *dva)
1657 {
1658         uint64_t ms_shift = msp->ms_group->mg_vd->vdev_ms_shift;
1659         uint64_t offset = DVA_GET_OFFSET(dva) >> ms_shift;
1660         uint64_t start = msp->ms_id;
1661
1662         if (msp->ms_group->mg_vd->vdev_id != DVA_GET_VDEV(dva))
1663                 return (1ULL << 63);
1664
1665         if (offset < start)
1666                 return ((start - offset) << ms_shift);
1667         if (offset > start)
1668                 return ((offset - start) << ms_shift);
1669         return (0);
1670 }
1671
1672 static uint64_t
1673 metaslab_group_alloc(metaslab_group_t *mg, uint64_t psize, uint64_t asize,
1674     uint64_t txg, uint64_t min_distance, dva_t *dva, int d, int flags)
1675 {
1676         spa_t *spa = mg->mg_vd->vdev_spa;
1677         metaslab_t *msp = NULL;
1678         uint64_t offset = -1ULL;
1679         avl_tree_t *t = &mg->mg_metaslab_tree;
1680         uint64_t activation_weight;
1681         uint64_t target_distance;
1682         int i;
1683
1684         activation_weight = METASLAB_WEIGHT_PRIMARY;
1685         for (i = 0; i < d; i++) {
1686                 if (DVA_GET_VDEV(&dva[i]) == mg->mg_vd->vdev_id) {
1687                         activation_weight = METASLAB_WEIGHT_SECONDARY;
1688                         break;
1689                 }
1690         }
1691
1692         for (;;) {
1693                 boolean_t was_active;
1694
1695                 mutex_enter(&mg->mg_lock);
1696                 for (msp = avl_first(t); msp; msp = AVL_NEXT(t, msp)) {
1697                         if (msp->ms_weight < asize) {
1698                                 spa_dbgmsg(spa, "%s: failed to meet weight "
1699                                     "requirement: vdev %llu, txg %llu, mg %p, "
1700                                     "msp %p, psize %llu, asize %llu, "
1701                                     "failures %llu, weight %llu",
1702                                     spa_name(spa), mg->mg_vd->vdev_id, txg,
1703                                     mg, msp, psize, asize,
1704                                     mg->mg_alloc_failures, msp->ms_weight);
1705                                 mutex_exit(&mg->mg_lock);
1706                                 return (-1ULL);
1707                         }
1708
1709                         /*
1710                          * If the selected metaslab is condensing, skip it.
1711                          */
1712                         if (msp->ms_condensing)
1713                                 continue;
1714
1715                         was_active = msp->ms_weight & METASLAB_ACTIVE_MASK;
1716                         if (activation_weight == METASLAB_WEIGHT_PRIMARY)
1717                                 break;
1718
1719                         target_distance = min_distance +
1720                             (space_map_allocated(msp->ms_sm) != 0 ? 0 :
1721                             min_distance >> 1);
1722
1723                         for (i = 0; i < d; i++)
1724                                 if (metaslab_distance(msp, &dva[i]) <
1725                                     target_distance)
1726                                         break;
1727                         if (i == d)
1728                                 break;
1729                 }
1730                 mutex_exit(&mg->mg_lock);
1731                 if (msp == NULL)
1732                         return (-1ULL);
1733
1734                 mutex_enter(&msp->ms_lock);
1735
1736                 /*
1737                  * If we've already reached the allowable number of failed
1738                  * allocation attempts on this metaslab group then we
1739                  * consider skipping it. We skip it only if we're allowed
1740                  * to "fast" gang, the physical size is larger than
1741                  * a gang block, and we're attempting to allocate from
1742                  * the primary metaslab.
1743                  */
1744                 if (mg->mg_alloc_failures > zfs_mg_alloc_failures &&
1745                     CAN_FASTGANG(flags) && psize > SPA_GANGBLOCKSIZE &&
1746                     activation_weight == METASLAB_WEIGHT_PRIMARY) {
1747                         spa_dbgmsg(spa, "%s: skipping metaslab group: "
1748                             "vdev %llu, txg %llu, mg %p, msp[%llu] %p, "
1749                             "psize %llu, asize %llu, failures %llu",
1750                             spa_name(spa), mg->mg_vd->vdev_id, txg, mg,
1751                             msp->ms_id, msp, psize, asize,
1752                             mg->mg_alloc_failures);
1753                         mutex_exit(&msp->ms_lock);
1754                         return (-1ULL);
1755                 }
1756
1757                 /*
1758                  * Ensure that the metaslab we have selected is still
1759                  * capable of handling our request. It's possible that
1760                  * another thread may have changed the weight while we
1761                  * were blocked on the metaslab lock.
1762                  */
1763                 if (msp->ms_weight < asize || (was_active &&
1764                     !(msp->ms_weight & METASLAB_ACTIVE_MASK) &&
1765                     activation_weight == METASLAB_WEIGHT_PRIMARY)) {
1766                         mutex_exit(&msp->ms_lock);
1767                         continue;
1768                 }
1769
1770                 if ((msp->ms_weight & METASLAB_WEIGHT_SECONDARY) &&
1771                     activation_weight == METASLAB_WEIGHT_PRIMARY) {
1772                         metaslab_passivate(msp,
1773                             msp->ms_weight & ~METASLAB_ACTIVE_MASK);
1774                         mutex_exit(&msp->ms_lock);
1775                         continue;
1776                 }
1777
1778                 if (metaslab_activate(msp, activation_weight) != 0) {
1779                         mutex_exit(&msp->ms_lock);
1780                         continue;
1781                 }
1782
1783                 /*
1784                  * If this metaslab is currently condensing then pick again as
1785                  * we can't manipulate this metaslab until it's committed
1786                  * to disk.
1787                  */
1788                 if (msp->ms_condensing) {
1789                         mutex_exit(&msp->ms_lock);
1790                         continue;
1791                 }
1792
1793                 if ((offset = metaslab_block_alloc(msp, asize)) != -1ULL)
1794                         break;
1795
1796                 atomic_inc_64(&mg->mg_alloc_failures);
1797
1798                 metaslab_passivate(msp, metaslab_block_maxsize(msp));
1799                 mutex_exit(&msp->ms_lock);
1800         }
1801
1802         if (range_tree_space(msp->ms_alloctree[txg & TXG_MASK]) == 0)
1803                 vdev_dirty(mg->mg_vd, VDD_METASLAB, msp, txg);
1804
1805         range_tree_add(msp->ms_alloctree[txg & TXG_MASK], offset, asize);
1806         msp->ms_access_txg = txg + metaslab_unload_delay;
1807
1808         mutex_exit(&msp->ms_lock);
1809
1810         return (offset);
1811 }
1812
1813 /*
1814  * Allocate a block for the specified i/o.
1815  */
1816 static int
1817 metaslab_alloc_dva(spa_t *spa, metaslab_class_t *mc, uint64_t psize,
1818     dva_t *dva, int d, dva_t *hintdva, uint64_t txg, int flags)
1819 {
1820         metaslab_group_t *mg, *fast_mg, *rotor;
1821         vdev_t *vd;
1822         int dshift = 3;
1823         int all_zero;
1824         int zio_lock = B_FALSE;
1825         boolean_t allocatable;
1826         uint64_t offset = -1ULL;
1827         uint64_t asize;
1828         uint64_t distance;
1829
1830         ASSERT(!DVA_IS_VALID(&dva[d]));
1831
1832         /*
1833          * For testing, make some blocks above a certain size be gang blocks.
1834          */
1835         if (psize >= metaslab_gang_bang && (ddi_get_lbolt() & 3) == 0)
1836                 return (SET_ERROR(ENOSPC));
1837
1838         if (flags & METASLAB_FASTWRITE)
1839                 mutex_enter(&mc->mc_fastwrite_lock);
1840
1841         /*
1842          * Start at the rotor and loop through all mgs until we find something.
1843          * Note that there's no locking on mc_rotor or mc_aliquot because
1844          * nothing actually breaks if we miss a few updates -- we just won't
1845          * allocate quite as evenly.  It all balances out over time.
1846          *
1847          * If we are doing ditto or log blocks, try to spread them across
1848          * consecutive vdevs.  If we're forced to reuse a vdev before we've
1849          * allocated all of our ditto blocks, then try and spread them out on
1850          * that vdev as much as possible.  If it turns out to not be possible,
1851          * gradually lower our standards until anything becomes acceptable.
1852          * Also, allocating on consecutive vdevs (as opposed to random vdevs)
1853          * gives us hope of containing our fault domains to something we're
1854          * able to reason about.  Otherwise, any two top-level vdev failures
1855          * will guarantee the loss of data.  With consecutive allocation,
1856          * only two adjacent top-level vdev failures will result in data loss.
1857          *
1858          * If we are doing gang blocks (hintdva is non-NULL), try to keep
1859          * ourselves on the same vdev as our gang block header.  That
1860          * way, we can hope for locality in vdev_cache, plus it makes our
1861          * fault domains something tractable.
1862          */
1863         if (hintdva) {
1864                 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&hintdva[d]));
1865
1866                 /*
1867                  * It's possible the vdev we're using as the hint no
1868                  * longer exists (i.e. removed). Consult the rotor when
1869                  * all else fails.
1870                  */
1871                 if (vd != NULL) {
1872                         mg = vd->vdev_mg;
1873
1874                         if (flags & METASLAB_HINTBP_AVOID &&
1875                             mg->mg_next != NULL)
1876                                 mg = mg->mg_next;
1877                 } else {
1878                         mg = mc->mc_rotor;
1879                 }
1880         } else if (d != 0) {
1881                 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d - 1]));
1882                 mg = vd->vdev_mg->mg_next;
1883         } else if (flags & METASLAB_FASTWRITE) {
1884                 mg = fast_mg = mc->mc_rotor;
1885
1886                 do {
1887                         if (fast_mg->mg_vd->vdev_pending_fastwrite <
1888                             mg->mg_vd->vdev_pending_fastwrite)
1889                                 mg = fast_mg;
1890                 } while ((fast_mg = fast_mg->mg_next) != mc->mc_rotor);
1891
1892         } else {
1893                 mg = mc->mc_rotor;
1894         }
1895
1896         /*
1897          * If the hint put us into the wrong metaslab class, or into a
1898          * metaslab group that has been passivated, just follow the rotor.
1899          */
1900         if (mg->mg_class != mc || mg->mg_activation_count <= 0)
1901                 mg = mc->mc_rotor;
1902
1903         rotor = mg;
1904 top:
1905         all_zero = B_TRUE;
1906         do {
1907                 ASSERT(mg->mg_activation_count == 1);
1908
1909                 vd = mg->mg_vd;
1910
1911                 /*
1912                  * Don't allocate from faulted devices.
1913                  */
1914                 if (zio_lock) {
1915                         spa_config_enter(spa, SCL_ZIO, FTAG, RW_READER);
1916                         allocatable = vdev_allocatable(vd);
1917                         spa_config_exit(spa, SCL_ZIO, FTAG);
1918                 } else {
1919                         allocatable = vdev_allocatable(vd);
1920                 }
1921
1922                 /*
1923                  * Determine if the selected metaslab group is eligible
1924                  * for allocations. If we're ganging or have requested
1925                  * an allocation for the smallest gang block size
1926                  * then we don't want to avoid allocating to the this
1927                  * metaslab group. If we're in this condition we should
1928                  * try to allocate from any device possible so that we
1929                  * don't inadvertently return ENOSPC and suspend the pool
1930                  * even though space is still available.
1931                  */
1932                 if (allocatable && CAN_FASTGANG(flags) &&
1933                     psize > SPA_GANGBLOCKSIZE)
1934                         allocatable = metaslab_group_allocatable(mg);
1935
1936                 if (!allocatable)
1937                         goto next;
1938
1939                 /*
1940                  * Avoid writing single-copy data to a failing vdev
1941                  * unless the user instructs us that it is okay.
1942                  */
1943                 if ((vd->vdev_stat.vs_write_errors > 0 ||
1944                     vd->vdev_state < VDEV_STATE_HEALTHY) &&
1945                     d == 0 && dshift == 3 &&
1946                     !(zfs_write_to_degraded && vd->vdev_state ==
1947                     VDEV_STATE_DEGRADED)) {
1948                         all_zero = B_FALSE;
1949                         goto next;
1950                 }
1951
1952                 ASSERT(mg->mg_class == mc);
1953
1954                 distance = vd->vdev_asize >> dshift;
1955                 if (distance <= (1ULL << vd->vdev_ms_shift))
1956                         distance = 0;
1957                 else
1958                         all_zero = B_FALSE;
1959
1960                 asize = vdev_psize_to_asize(vd, psize);
1961                 ASSERT(P2PHASE(asize, 1ULL << vd->vdev_ashift) == 0);
1962
1963                 offset = metaslab_group_alloc(mg, psize, asize, txg, distance,
1964                     dva, d, flags);
1965                 if (offset != -1ULL) {
1966                         /*
1967                          * If we've just selected this metaslab group,
1968                          * figure out whether the corresponding vdev is
1969                          * over- or under-used relative to the pool,
1970                          * and set an allocation bias to even it out.
1971                          */
1972                         if (mc->mc_aliquot == 0) {
1973                                 vdev_stat_t *vs = &vd->vdev_stat;
1974                                 int64_t vu, cu;
1975
1976                                 vu = (vs->vs_alloc * 100) / (vs->vs_space + 1);
1977                                 cu = (mc->mc_alloc * 100) / (mc->mc_space + 1);
1978
1979                                 /*
1980                                  * Calculate how much more or less we should
1981                                  * try to allocate from this device during
1982                                  * this iteration around the rotor.
1983                                  * For example, if a device is 80% full
1984                                  * and the pool is 20% full then we should
1985                                  * reduce allocations by 60% on this device.
1986                                  *
1987                                  * mg_bias = (20 - 80) * 512K / 100 = -307K
1988                                  *
1989                                  * This reduces allocations by 307K for this
1990                                  * iteration.
1991                                  */
1992                                 mg->mg_bias = ((cu - vu) *
1993                                     (int64_t)mg->mg_aliquot) / 100;
1994                         }
1995
1996                         if ((flags & METASLAB_FASTWRITE) ||
1997                             atomic_add_64_nv(&mc->mc_aliquot, asize) >=
1998                             mg->mg_aliquot + mg->mg_bias) {
1999                                 mc->mc_rotor = mg->mg_next;
2000                                 mc->mc_aliquot = 0;
2001                         }
2002
2003                         DVA_SET_VDEV(&dva[d], vd->vdev_id);
2004                         DVA_SET_OFFSET(&dva[d], offset);
2005                         DVA_SET_GANG(&dva[d], !!(flags & METASLAB_GANG_HEADER));
2006                         DVA_SET_ASIZE(&dva[d], asize);
2007
2008                         if (flags & METASLAB_FASTWRITE) {
2009                                 atomic_add_64(&vd->vdev_pending_fastwrite,
2010                                     psize);
2011                                 mutex_exit(&mc->mc_fastwrite_lock);
2012                         }
2013
2014                         return (0);
2015                 }
2016 next:
2017                 mc->mc_rotor = mg->mg_next;
2018                 mc->mc_aliquot = 0;
2019         } while ((mg = mg->mg_next) != rotor);
2020
2021         if (!all_zero) {
2022                 dshift++;
2023                 ASSERT(dshift < 64);
2024                 goto top;
2025         }
2026
2027         if (!allocatable && !zio_lock) {
2028                 dshift = 3;
2029                 zio_lock = B_TRUE;
2030                 goto top;
2031         }
2032
2033         bzero(&dva[d], sizeof (dva_t));
2034
2035         if (flags & METASLAB_FASTWRITE)
2036                 mutex_exit(&mc->mc_fastwrite_lock);
2037
2038         return (SET_ERROR(ENOSPC));
2039 }
2040
2041 /*
2042  * Free the block represented by DVA in the context of the specified
2043  * transaction group.
2044  */
2045 static void
2046 metaslab_free_dva(spa_t *spa, const dva_t *dva, uint64_t txg, boolean_t now)
2047 {
2048         uint64_t vdev = DVA_GET_VDEV(dva);
2049         uint64_t offset = DVA_GET_OFFSET(dva);
2050         uint64_t size = DVA_GET_ASIZE(dva);
2051         vdev_t *vd;
2052         metaslab_t *msp;
2053
2054         ASSERT(DVA_IS_VALID(dva));
2055
2056         if (txg > spa_freeze_txg(spa))
2057                 return;
2058
2059         if ((vd = vdev_lookup_top(spa, vdev)) == NULL ||
2060             (offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count) {
2061                 cmn_err(CE_WARN, "metaslab_free_dva(): bad DVA %llu:%llu",
2062                     (u_longlong_t)vdev, (u_longlong_t)offset);
2063                 ASSERT(0);
2064                 return;
2065         }
2066
2067         msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
2068
2069         if (DVA_GET_GANG(dva))
2070                 size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
2071
2072         mutex_enter(&msp->ms_lock);
2073
2074         if (now) {
2075                 range_tree_remove(msp->ms_alloctree[txg & TXG_MASK],
2076                     offset, size);
2077
2078                 VERIFY(!msp->ms_condensing);
2079                 VERIFY3U(offset, >=, msp->ms_start);
2080                 VERIFY3U(offset + size, <=, msp->ms_start + msp->ms_size);
2081                 VERIFY3U(range_tree_space(msp->ms_tree) + size, <=,
2082                     msp->ms_size);
2083                 VERIFY0(P2PHASE(offset, 1ULL << vd->vdev_ashift));
2084                 VERIFY0(P2PHASE(size, 1ULL << vd->vdev_ashift));
2085                 range_tree_add(msp->ms_tree, offset, size);
2086         } else {
2087                 if (range_tree_space(msp->ms_freetree[txg & TXG_MASK]) == 0)
2088                         vdev_dirty(vd, VDD_METASLAB, msp, txg);
2089                 range_tree_add(msp->ms_freetree[txg & TXG_MASK],
2090                     offset, size);
2091         }
2092
2093         mutex_exit(&msp->ms_lock);
2094 }
2095
2096 /*
2097  * Intent log support: upon opening the pool after a crash, notify the SPA
2098  * of blocks that the intent log has allocated for immediate write, but
2099  * which are still considered free by the SPA because the last transaction
2100  * group didn't commit yet.
2101  */
2102 static int
2103 metaslab_claim_dva(spa_t *spa, const dva_t *dva, uint64_t txg)
2104 {
2105         uint64_t vdev = DVA_GET_VDEV(dva);
2106         uint64_t offset = DVA_GET_OFFSET(dva);
2107         uint64_t size = DVA_GET_ASIZE(dva);
2108         vdev_t *vd;
2109         metaslab_t *msp;
2110         int error = 0;
2111
2112         ASSERT(DVA_IS_VALID(dva));
2113
2114         if ((vd = vdev_lookup_top(spa, vdev)) == NULL ||
2115             (offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count)
2116                 return (SET_ERROR(ENXIO));
2117
2118         msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
2119
2120         if (DVA_GET_GANG(dva))
2121                 size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
2122
2123         mutex_enter(&msp->ms_lock);
2124
2125         if ((txg != 0 && spa_writeable(spa)) || !msp->ms_loaded)
2126                 error = metaslab_activate(msp, METASLAB_WEIGHT_SECONDARY);
2127
2128         if (error == 0 && !range_tree_contains(msp->ms_tree, offset, size))
2129                 error = SET_ERROR(ENOENT);
2130
2131         if (error || txg == 0) {        /* txg == 0 indicates dry run */
2132                 mutex_exit(&msp->ms_lock);
2133                 return (error);
2134         }
2135
2136         VERIFY(!msp->ms_condensing);
2137         VERIFY0(P2PHASE(offset, 1ULL << vd->vdev_ashift));
2138         VERIFY0(P2PHASE(size, 1ULL << vd->vdev_ashift));
2139         VERIFY3U(range_tree_space(msp->ms_tree) - size, <=, msp->ms_size);
2140         range_tree_remove(msp->ms_tree, offset, size);
2141
2142         if (spa_writeable(spa)) {       /* don't dirty if we're zdb(1M) */
2143                 if (range_tree_space(msp->ms_alloctree[txg & TXG_MASK]) == 0)
2144                         vdev_dirty(vd, VDD_METASLAB, msp, txg);
2145                 range_tree_add(msp->ms_alloctree[txg & TXG_MASK], offset, size);
2146         }
2147
2148         mutex_exit(&msp->ms_lock);
2149
2150         return (0);
2151 }
2152
2153 int
2154 metaslab_alloc(spa_t *spa, metaslab_class_t *mc, uint64_t psize, blkptr_t *bp,
2155     int ndvas, uint64_t txg, blkptr_t *hintbp, int flags)
2156 {
2157         dva_t *dva = bp->blk_dva;
2158         dva_t *hintdva = hintbp->blk_dva;
2159         int d, error = 0;
2160
2161         ASSERT(bp->blk_birth == 0);
2162         ASSERT(BP_PHYSICAL_BIRTH(bp) == 0);
2163
2164         spa_config_enter(spa, SCL_ALLOC, FTAG, RW_READER);
2165
2166         if (mc->mc_rotor == NULL) {     /* no vdevs in this class */
2167                 spa_config_exit(spa, SCL_ALLOC, FTAG);
2168                 return (SET_ERROR(ENOSPC));
2169         }
2170
2171         ASSERT(ndvas > 0 && ndvas <= spa_max_replication(spa));
2172         ASSERT(BP_GET_NDVAS(bp) == 0);
2173         ASSERT(hintbp == NULL || ndvas <= BP_GET_NDVAS(hintbp));
2174
2175         for (d = 0; d < ndvas; d++) {
2176                 error = metaslab_alloc_dva(spa, mc, psize, dva, d, hintdva,
2177                     txg, flags);
2178                 if (error != 0) {
2179                         for (d--; d >= 0; d--) {
2180                                 metaslab_free_dva(spa, &dva[d], txg, B_TRUE);
2181                                 bzero(&dva[d], sizeof (dva_t));
2182                         }
2183                         spa_config_exit(spa, SCL_ALLOC, FTAG);
2184                         return (error);
2185                 }
2186         }
2187         ASSERT(error == 0);
2188         ASSERT(BP_GET_NDVAS(bp) == ndvas);
2189
2190         spa_config_exit(spa, SCL_ALLOC, FTAG);
2191
2192         BP_SET_BIRTH(bp, txg, txg);
2193
2194         return (0);
2195 }
2196
2197 void
2198 metaslab_free(spa_t *spa, const blkptr_t *bp, uint64_t txg, boolean_t now)
2199 {
2200         const dva_t *dva = bp->blk_dva;
2201         int d, ndvas = BP_GET_NDVAS(bp);
2202
2203         ASSERT(!BP_IS_HOLE(bp));
2204         ASSERT(!now || bp->blk_birth >= spa_syncing_txg(spa));
2205
2206         spa_config_enter(spa, SCL_FREE, FTAG, RW_READER);
2207
2208         for (d = 0; d < ndvas; d++)
2209                 metaslab_free_dva(spa, &dva[d], txg, now);
2210
2211         spa_config_exit(spa, SCL_FREE, FTAG);
2212 }
2213
2214 int
2215 metaslab_claim(spa_t *spa, const blkptr_t *bp, uint64_t txg)
2216 {
2217         const dva_t *dva = bp->blk_dva;
2218         int ndvas = BP_GET_NDVAS(bp);
2219         int d, error = 0;
2220
2221         ASSERT(!BP_IS_HOLE(bp));
2222
2223         if (txg != 0) {
2224                 /*
2225                  * First do a dry run to make sure all DVAs are claimable,
2226                  * so we don't have to unwind from partial failures below.
2227                  */
2228                 if ((error = metaslab_claim(spa, bp, 0)) != 0)
2229                         return (error);
2230         }
2231
2232         spa_config_enter(spa, SCL_ALLOC, FTAG, RW_READER);
2233
2234         for (d = 0; d < ndvas; d++)
2235                 if ((error = metaslab_claim_dva(spa, &dva[d], txg)) != 0)
2236                         break;
2237
2238         spa_config_exit(spa, SCL_ALLOC, FTAG);
2239
2240         ASSERT(error == 0 || txg == 0);
2241
2242         return (error);
2243 }
2244
2245 void
2246 metaslab_fastwrite_mark(spa_t *spa, const blkptr_t *bp)
2247 {
2248         const dva_t *dva = bp->blk_dva;
2249         int ndvas = BP_GET_NDVAS(bp);
2250         uint64_t psize = BP_GET_PSIZE(bp);
2251         int d;
2252         vdev_t *vd;
2253
2254         ASSERT(!BP_IS_HOLE(bp));
2255         ASSERT(psize > 0);
2256
2257         spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2258
2259         for (d = 0; d < ndvas; d++) {
2260                 if ((vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d]))) == NULL)
2261                         continue;
2262                 atomic_add_64(&vd->vdev_pending_fastwrite, psize);
2263         }
2264
2265         spa_config_exit(spa, SCL_VDEV, FTAG);
2266 }
2267
2268 void
2269 metaslab_fastwrite_unmark(spa_t *spa, const blkptr_t *bp)
2270 {
2271         const dva_t *dva = bp->blk_dva;
2272         int ndvas = BP_GET_NDVAS(bp);
2273         uint64_t psize = BP_GET_PSIZE(bp);
2274         int d;
2275         vdev_t *vd;
2276
2277         ASSERT(!BP_IS_HOLE(bp));
2278         ASSERT(psize > 0);
2279
2280         spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2281
2282         for (d = 0; d < ndvas; d++) {
2283                 if ((vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d]))) == NULL)
2284                         continue;
2285                 ASSERT3U(vd->vdev_pending_fastwrite, >=, psize);
2286                 atomic_sub_64(&vd->vdev_pending_fastwrite, psize);
2287         }
2288
2289         spa_config_exit(spa, SCL_VDEV, FTAG);
2290 }
2291
2292 void
2293 metaslab_check_free(spa_t *spa, const blkptr_t *bp)
2294 {
2295         int i, j;
2296
2297         if ((zfs_flags & ZFS_DEBUG_ZIO_FREE) == 0)
2298                 return;
2299
2300         spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2301         for (i = 0; i < BP_GET_NDVAS(bp); i++) {
2302                 uint64_t vdev = DVA_GET_VDEV(&bp->blk_dva[i]);
2303                 vdev_t *vd = vdev_lookup_top(spa, vdev);
2304                 uint64_t offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
2305                 uint64_t size = DVA_GET_ASIZE(&bp->blk_dva[i]);
2306                 metaslab_t *msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
2307
2308                 if (msp->ms_loaded)
2309                         range_tree_verify(msp->ms_tree, offset, size);
2310
2311                 for (j = 0; j < TXG_SIZE; j++)
2312                         range_tree_verify(msp->ms_freetree[j], offset, size);
2313                 for (j = 0; j < TXG_DEFER_SIZE; j++)
2314                         range_tree_verify(msp->ms_defertree[j], offset, size);
2315         }
2316         spa_config_exit(spa, SCL_VDEV, FTAG);
2317 }
2318
2319 #if defined(_KERNEL) && defined(HAVE_SPL)
2320 module_param(metaslab_debug_load, int, 0644);
2321 module_param(metaslab_debug_unload, int, 0644);
2322 MODULE_PARM_DESC(metaslab_debug_load,
2323         "load all metaslabs when pool is first opened");
2324 MODULE_PARM_DESC(metaslab_debug_unload,
2325         "prevent metaslabs from being unloaded");
2326
2327 module_param(zfs_mg_noalloc_threshold, int, 0644);
2328 MODULE_PARM_DESC(zfs_mg_noalloc_threshold,
2329         "percentage of free space for metaslab group to allow allocation");
2330 #endif /* _KERNEL && HAVE_SPL */