]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/metaslab.c
MFC r211931:
[FreeBSD/stable/8.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  */
24
25 #include <sys/zfs_context.h>
26 #include <sys/spa_impl.h>
27 #include <sys/dmu.h>
28 #include <sys/dmu_tx.h>
29 #include <sys/space_map.h>
30 #include <sys/metaslab_impl.h>
31 #include <sys/vdev_impl.h>
32 #include <sys/zio.h>
33
34 uint64_t metaslab_aliquot = 512ULL << 10;
35 uint64_t metaslab_gang_bang = SPA_MAXBLOCKSIZE + 1;     /* force gang blocks */
36
37 /*
38  * Minimum size which forces the dynamic allocator to change
39  * it's allocation strategy.  Once the space map cannot satisfy
40  * an allocation of this size then it switches to using more
41  * aggressive strategy (i.e search by size rather than offset).
42  */
43 uint64_t metaslab_df_alloc_threshold = SPA_MAXBLOCKSIZE;
44
45 /*
46  * The minimum free space, in percent, which must be available
47  * in a space map to continue allocations in a first-fit fashion.
48  * Once the space_map's free space drops below this level we dynamically
49  * switch to using best-fit allocations.
50  */
51 int metaslab_df_free_pct = 4;
52
53 /*
54  * A metaslab is considered "free" if it contains a contiguous
55  * segment which is greater than metaslab_min_alloc_size.
56  */
57 uint64_t metaslab_min_alloc_size = DMU_MAX_ACCESS;
58
59 /*
60  * Max number of space_maps to prefetch.
61  */
62 int metaslab_prefetch_limit = SPA_DVAS_PER_BP;
63
64 /*
65  * Percentage bonus multiplier for metaslabs that are in the bonus area.
66  */
67 int metaslab_smo_bonus_pct = 150;
68
69 /*
70  * ==========================================================================
71  * Metaslab classes
72  * ==========================================================================
73  */
74 metaslab_class_t *
75 metaslab_class_create(space_map_ops_t *ops)
76 {
77         metaslab_class_t *mc;
78
79         mc = kmem_zalloc(sizeof (metaslab_class_t), KM_SLEEP);
80
81         mc->mc_rotor = NULL;
82         mc->mc_ops = ops;
83
84         return (mc);
85 }
86
87 void
88 metaslab_class_destroy(metaslab_class_t *mc)
89 {
90         metaslab_group_t *mg;
91
92         while ((mg = mc->mc_rotor) != NULL) {
93                 metaslab_class_remove(mc, mg);
94                 metaslab_group_destroy(mg);
95         }
96
97         kmem_free(mc, sizeof (metaslab_class_t));
98 }
99
100 void
101 metaslab_class_add(metaslab_class_t *mc, metaslab_group_t *mg)
102 {
103         metaslab_group_t *mgprev, *mgnext;
104
105         ASSERT(mg->mg_class == NULL);
106
107         if ((mgprev = mc->mc_rotor) == NULL) {
108                 mg->mg_prev = mg;
109                 mg->mg_next = mg;
110         } else {
111                 mgnext = mgprev->mg_next;
112                 mg->mg_prev = mgprev;
113                 mg->mg_next = mgnext;
114                 mgprev->mg_next = mg;
115                 mgnext->mg_prev = mg;
116         }
117         mc->mc_rotor = mg;
118         mg->mg_class = mc;
119 }
120
121 void
122 metaslab_class_remove(metaslab_class_t *mc, metaslab_group_t *mg)
123 {
124         metaslab_group_t *mgprev, *mgnext;
125
126         ASSERT(mg->mg_class == mc);
127
128         mgprev = mg->mg_prev;
129         mgnext = mg->mg_next;
130
131         if (mg == mgnext) {
132                 mc->mc_rotor = NULL;
133         } else {
134                 mc->mc_rotor = mgnext;
135                 mgprev->mg_next = mgnext;
136                 mgnext->mg_prev = mgprev;
137         }
138
139         mg->mg_prev = NULL;
140         mg->mg_next = NULL;
141         mg->mg_class = NULL;
142 }
143
144 /*
145  * ==========================================================================
146  * Metaslab groups
147  * ==========================================================================
148  */
149 static int
150 metaslab_compare(const void *x1, const void *x2)
151 {
152         const metaslab_t *m1 = x1;
153         const metaslab_t *m2 = x2;
154
155         if (m1->ms_weight < m2->ms_weight)
156                 return (1);
157         if (m1->ms_weight > m2->ms_weight)
158                 return (-1);
159
160         /*
161          * If the weights are identical, use the offset to force uniqueness.
162          */
163         if (m1->ms_map.sm_start < m2->ms_map.sm_start)
164                 return (-1);
165         if (m1->ms_map.sm_start > m2->ms_map.sm_start)
166                 return (1);
167
168         ASSERT3P(m1, ==, m2);
169
170         return (0);
171 }
172
173 metaslab_group_t *
174 metaslab_group_create(metaslab_class_t *mc, vdev_t *vd)
175 {
176         metaslab_group_t *mg;
177
178         mg = kmem_zalloc(sizeof (metaslab_group_t), KM_SLEEP);
179         mutex_init(&mg->mg_lock, NULL, MUTEX_DEFAULT, NULL);
180         avl_create(&mg->mg_metaslab_tree, metaslab_compare,
181             sizeof (metaslab_t), offsetof(struct metaslab, ms_group_node));
182         mg->mg_aliquot = metaslab_aliquot * MAX(1, vd->vdev_children);
183         mg->mg_vd = vd;
184         metaslab_class_add(mc, mg);
185
186         return (mg);
187 }
188
189 void
190 metaslab_group_destroy(metaslab_group_t *mg)
191 {
192         avl_destroy(&mg->mg_metaslab_tree);
193         mutex_destroy(&mg->mg_lock);
194         kmem_free(mg, sizeof (metaslab_group_t));
195 }
196
197 static void
198 metaslab_group_add(metaslab_group_t *mg, metaslab_t *msp)
199 {
200         mutex_enter(&mg->mg_lock);
201         ASSERT(msp->ms_group == NULL);
202         msp->ms_group = mg;
203         msp->ms_weight = 0;
204         avl_add(&mg->mg_metaslab_tree, msp);
205         mutex_exit(&mg->mg_lock);
206 }
207
208 static void
209 metaslab_group_remove(metaslab_group_t *mg, metaslab_t *msp)
210 {
211         mutex_enter(&mg->mg_lock);
212         ASSERT(msp->ms_group == mg);
213         avl_remove(&mg->mg_metaslab_tree, msp);
214         msp->ms_group = NULL;
215         mutex_exit(&mg->mg_lock);
216 }
217
218 static void
219 metaslab_group_sort(metaslab_group_t *mg, metaslab_t *msp, uint64_t weight)
220 {
221         /*
222          * Although in principle the weight can be any value, in
223          * practice we do not use values in the range [1, 510].
224          */
225         ASSERT(weight >= SPA_MINBLOCKSIZE-1 || weight == 0);
226         ASSERT(MUTEX_HELD(&msp->ms_lock));
227
228         mutex_enter(&mg->mg_lock);
229         ASSERT(msp->ms_group == mg);
230         avl_remove(&mg->mg_metaslab_tree, msp);
231         msp->ms_weight = weight;
232         avl_add(&mg->mg_metaslab_tree, msp);
233         mutex_exit(&mg->mg_lock);
234 }
235
236 /*
237  * ==========================================================================
238  * Common allocator routines
239  * ==========================================================================
240  */
241 static int
242 metaslab_segsize_compare(const void *x1, const void *x2)
243 {
244         const space_seg_t *s1 = x1;
245         const space_seg_t *s2 = x2;
246         uint64_t ss_size1 = s1->ss_end - s1->ss_start;
247         uint64_t ss_size2 = s2->ss_end - s2->ss_start;
248
249         if (ss_size1 < ss_size2)
250                 return (-1);
251         if (ss_size1 > ss_size2)
252                 return (1);
253
254         if (s1->ss_start < s2->ss_start)
255                 return (-1);
256         if (s1->ss_start > s2->ss_start)
257                 return (1);
258
259         return (0);
260 }
261
262 /*
263  * This is a helper function that can be used by the allocator to find
264  * a suitable block to allocate. This will search the specified AVL
265  * tree looking for a block that matches the specified criteria.
266  */
267 static uint64_t
268 metaslab_block_picker(avl_tree_t *t, uint64_t *cursor, uint64_t size,
269     uint64_t align)
270 {
271         space_seg_t *ss, ssearch;
272         avl_index_t where;
273
274         ssearch.ss_start = *cursor;
275         ssearch.ss_end = *cursor + size;
276
277         ss = avl_find(t, &ssearch, &where);
278         if (ss == NULL)
279                 ss = avl_nearest(t, where, AVL_AFTER);
280
281         while (ss != NULL) {
282                 uint64_t offset = P2ROUNDUP(ss->ss_start, align);
283
284                 if (offset + size <= ss->ss_end) {
285                         *cursor = offset + size;
286                         return (offset);
287                 }
288                 ss = AVL_NEXT(t, ss);
289         }
290
291         /*
292          * If we know we've searched the whole map (*cursor == 0), give up.
293          * Otherwise, reset the cursor to the beginning and try again.
294          */
295         if (*cursor == 0)
296                 return (-1ULL);
297
298         *cursor = 0;
299         return (metaslab_block_picker(t, cursor, size, align));
300 }
301
302 static void
303 metaslab_pp_load(space_map_t *sm)
304 {
305         space_seg_t *ss;
306
307         ASSERT(sm->sm_ppd == NULL);
308         sm->sm_ppd = kmem_zalloc(64 * sizeof (uint64_t), KM_SLEEP);
309
310         sm->sm_pp_root = kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
311         avl_create(sm->sm_pp_root, metaslab_segsize_compare,
312             sizeof (space_seg_t), offsetof(struct space_seg, ss_pp_node));
313
314         for (ss = avl_first(&sm->sm_root); ss; ss = AVL_NEXT(&sm->sm_root, ss))
315                 avl_add(sm->sm_pp_root, ss);
316 }
317
318 static void
319 metaslab_pp_unload(space_map_t *sm)
320 {
321         void *cookie = NULL;
322
323         kmem_free(sm->sm_ppd, 64 * sizeof (uint64_t));
324         sm->sm_ppd = NULL;
325
326         while (avl_destroy_nodes(sm->sm_pp_root, &cookie) != NULL) {
327                 /* tear down the tree */
328         }
329
330         avl_destroy(sm->sm_pp_root);
331         kmem_free(sm->sm_pp_root, sizeof (avl_tree_t));
332         sm->sm_pp_root = NULL;
333 }
334
335 /* ARGSUSED */
336 static void
337 metaslab_pp_claim(space_map_t *sm, uint64_t start, uint64_t size)
338 {
339         /* No need to update cursor */
340 }
341
342 /* ARGSUSED */
343 static void
344 metaslab_pp_free(space_map_t *sm, uint64_t start, uint64_t size)
345 {
346         /* No need to update cursor */
347 }
348
349 /*
350  * Return the maximum contiguous segment within the metaslab.
351  */
352 uint64_t
353 metaslab_pp_maxsize(space_map_t *sm)
354 {
355         avl_tree_t *t = sm->sm_pp_root;
356         space_seg_t *ss;
357
358         if (t == NULL || (ss = avl_last(t)) == NULL)
359                 return (0ULL);
360
361         return (ss->ss_end - ss->ss_start);
362 }
363
364 /*
365  * ==========================================================================
366  * The first-fit block allocator
367  * ==========================================================================
368  */
369 static uint64_t
370 metaslab_ff_alloc(space_map_t *sm, uint64_t size)
371 {
372         avl_tree_t *t = &sm->sm_root;
373         uint64_t align = size & -size;
374         uint64_t *cursor = (uint64_t *)sm->sm_ppd + highbit(align) - 1;
375
376         return (metaslab_block_picker(t, cursor, size, align));
377 }
378
379 /* ARGSUSED */
380 boolean_t
381 metaslab_ff_fragmented(space_map_t *sm)
382 {
383         return (B_TRUE);
384 }
385
386 static space_map_ops_t metaslab_ff_ops = {
387         metaslab_pp_load,
388         metaslab_pp_unload,
389         metaslab_ff_alloc,
390         metaslab_pp_claim,
391         metaslab_pp_free,
392         metaslab_pp_maxsize,
393         metaslab_ff_fragmented
394 };
395
396 /*
397  * ==========================================================================
398  * Dynamic block allocator -
399  * Uses the first fit allocation scheme until space get low and then
400  * adjusts to a best fit allocation method. Uses metaslab_df_alloc_threshold
401  * and metaslab_df_free_pct to determine when to switch the allocation scheme.
402  * ==========================================================================
403  */
404 static uint64_t
405 metaslab_df_alloc(space_map_t *sm, uint64_t size)
406 {
407         avl_tree_t *t = &sm->sm_root;
408         uint64_t align = size & -size;
409         uint64_t *cursor = (uint64_t *)sm->sm_ppd + highbit(align) - 1;
410         uint64_t max_size = metaslab_pp_maxsize(sm);
411         int free_pct = sm->sm_space * 100 / sm->sm_size;
412
413         ASSERT(MUTEX_HELD(sm->sm_lock));
414         ASSERT3U(avl_numnodes(&sm->sm_root), ==, avl_numnodes(sm->sm_pp_root));
415
416         if (max_size < size)
417                 return (-1ULL);
418
419         /*
420          * If we're running low on space switch to using the size
421          * sorted AVL tree (best-fit).
422          */
423         if (max_size < metaslab_df_alloc_threshold ||
424             free_pct < metaslab_df_free_pct) {
425                 t = sm->sm_pp_root;
426                 *cursor = 0;
427         }
428
429         return (metaslab_block_picker(t, cursor, size, 1ULL));
430 }
431
432 static boolean_t
433 metaslab_df_fragmented(space_map_t *sm)
434 {
435         uint64_t max_size = metaslab_pp_maxsize(sm);
436         int free_pct = sm->sm_space * 100 / sm->sm_size;
437
438         if (max_size >= metaslab_df_alloc_threshold &&
439             free_pct >= metaslab_df_free_pct)
440                 return (B_FALSE);
441
442         return (B_TRUE);
443 }
444
445 static space_map_ops_t metaslab_df_ops = {
446         metaslab_pp_load,
447         metaslab_pp_unload,
448         metaslab_df_alloc,
449         metaslab_pp_claim,
450         metaslab_pp_free,
451         metaslab_pp_maxsize,
452         metaslab_df_fragmented
453 };
454
455 /*
456  * ==========================================================================
457  * Other experimental allocators
458  * ==========================================================================
459  */
460 static uint64_t
461 metaslab_cdf_alloc(space_map_t *sm, uint64_t size)
462 {
463         avl_tree_t *t = &sm->sm_root;
464         uint64_t *cursor = (uint64_t *)sm->sm_ppd;
465         uint64_t *extent_end = (uint64_t *)sm->sm_ppd + 1;
466         uint64_t max_size = metaslab_pp_maxsize(sm);
467         uint64_t rsize = size;
468         uint64_t offset = 0;
469
470         ASSERT(MUTEX_HELD(sm->sm_lock));
471         ASSERT3U(avl_numnodes(&sm->sm_root), ==, avl_numnodes(sm->sm_pp_root));
472
473         if (max_size < size)
474                 return (-1ULL);
475
476         ASSERT3U(*extent_end, >=, *cursor);
477
478         /*
479          * If we're running low on space switch to using the size
480          * sorted AVL tree (best-fit).
481          */
482         if ((*cursor + size) > *extent_end) {
483
484                 t = sm->sm_pp_root;
485                 *cursor = *extent_end = 0;
486
487                 if (max_size > 2 * SPA_MAXBLOCKSIZE)
488                         rsize = MIN(metaslab_min_alloc_size, max_size);
489                 offset = metaslab_block_picker(t, extent_end, rsize, 1ULL);
490                 if (offset != -1)
491                         *cursor = offset + size;
492         } else {
493                 offset = metaslab_block_picker(t, cursor, rsize, 1ULL);
494         }
495         ASSERT3U(*cursor, <=, *extent_end);
496         return (offset);
497 }
498
499 static boolean_t
500 metaslab_cdf_fragmented(space_map_t *sm)
501 {
502         uint64_t max_size = metaslab_pp_maxsize(sm);
503
504         if (max_size > (metaslab_min_alloc_size * 10))
505                 return (B_FALSE);
506         return (B_TRUE);
507 }
508
509 static space_map_ops_t metaslab_cdf_ops = {
510         metaslab_pp_load,
511         metaslab_pp_unload,
512         metaslab_cdf_alloc,
513         metaslab_pp_claim,
514         metaslab_pp_free,
515         metaslab_pp_maxsize,
516         metaslab_cdf_fragmented
517 };
518
519 uint64_t metaslab_ndf_clump_shift = 4;
520
521 static uint64_t
522 metaslab_ndf_alloc(space_map_t *sm, uint64_t size)
523 {
524         avl_tree_t *t = &sm->sm_root;
525         avl_index_t where;
526         space_seg_t *ss, ssearch;
527         uint64_t hbit = highbit(size);
528         uint64_t *cursor = (uint64_t *)sm->sm_ppd + hbit - 1;
529         uint64_t max_size = metaslab_pp_maxsize(sm);
530
531         ASSERT(MUTEX_HELD(sm->sm_lock));
532         ASSERT3U(avl_numnodes(&sm->sm_root), ==, avl_numnodes(sm->sm_pp_root));
533
534         if (max_size < size)
535                 return (-1ULL);
536
537         ssearch.ss_start = *cursor;
538         ssearch.ss_end = *cursor + size;
539
540         ss = avl_find(t, &ssearch, &where);
541         if (ss == NULL || (ss->ss_start + size > ss->ss_end)) {
542                 t = sm->sm_pp_root;
543
544                 ssearch.ss_start = 0;
545                 ssearch.ss_end = MIN(max_size,
546                     1ULL << (hbit + metaslab_ndf_clump_shift));
547                 ss = avl_find(t, &ssearch, &where);
548                 if (ss == NULL)
549                         ss = avl_nearest(t, where, AVL_AFTER);
550                 ASSERT(ss != NULL);
551         }
552
553         if (ss != NULL) {
554                 if (ss->ss_start + size <= ss->ss_end) {
555                         *cursor = ss->ss_start + size;
556                         return (ss->ss_start);
557                 }
558         }
559         return (-1ULL);
560 }
561
562 static boolean_t
563 metaslab_ndf_fragmented(space_map_t *sm)
564 {
565         uint64_t max_size = metaslab_pp_maxsize(sm);
566
567         if (max_size > (metaslab_min_alloc_size << metaslab_ndf_clump_shift))
568                 return (B_FALSE);
569         return (B_TRUE);
570 }
571
572
573 static space_map_ops_t metaslab_ndf_ops = {
574         metaslab_pp_load,
575         metaslab_pp_unload,
576         metaslab_ndf_alloc,
577         metaslab_pp_claim,
578         metaslab_pp_free,
579         metaslab_pp_maxsize,
580         metaslab_ndf_fragmented
581 };
582
583 space_map_ops_t *zfs_metaslab_ops = &metaslab_ndf_ops;
584
585 /*
586  * ==========================================================================
587  * Metaslabs
588  * ==========================================================================
589  */
590 metaslab_t *
591 metaslab_init(metaslab_group_t *mg, space_map_obj_t *smo,
592         uint64_t start, uint64_t size, uint64_t txg)
593 {
594         vdev_t *vd = mg->mg_vd;
595         metaslab_t *msp;
596
597         msp = kmem_zalloc(sizeof (metaslab_t), KM_SLEEP);
598         mutex_init(&msp->ms_lock, NULL, MUTEX_DEFAULT, NULL);
599
600         msp->ms_smo_syncing = *smo;
601
602         /*
603          * We create the main space map here, but we don't create the
604          * allocmaps and freemaps until metaslab_sync_done().  This serves
605          * two purposes: it allows metaslab_sync_done() to detect the
606          * addition of new space; and for debugging, it ensures that we'd
607          * data fault on any attempt to use this metaslab before it's ready.
608          */
609         space_map_create(&msp->ms_map, start, size,
610             vd->vdev_ashift, &msp->ms_lock);
611
612         metaslab_group_add(mg, msp);
613
614         /*
615          * If we're opening an existing pool (txg == 0) or creating
616          * a new one (txg == TXG_INITIAL), all space is available now.
617          * If we're adding space to an existing pool, the new space
618          * does not become available until after this txg has synced.
619          */
620         if (txg <= TXG_INITIAL)
621                 metaslab_sync_done(msp, 0);
622
623         if (txg != 0) {
624                 /*
625                  * The vdev is dirty, but the metaslab isn't -- it just needs
626                  * to have metaslab_sync_done() invoked from vdev_sync_done().
627                  * [We could just dirty the metaslab, but that would cause us
628                  * to allocate a space map object for it, which is wasteful
629                  * and would mess up the locality logic in metaslab_weight().]
630                  */
631                 ASSERT(TXG_CLEAN(txg) == spa_last_synced_txg(vd->vdev_spa));
632                 vdev_dirty(vd, 0, NULL, txg);
633                 vdev_dirty(vd, VDD_METASLAB, msp, TXG_CLEAN(txg));
634         }
635
636         return (msp);
637 }
638
639 void
640 metaslab_fini(metaslab_t *msp)
641 {
642         metaslab_group_t *mg = msp->ms_group;
643         int t;
644
645         vdev_space_update(mg->mg_vd, -msp->ms_map.sm_size,
646             -msp->ms_smo.smo_alloc, B_TRUE);
647
648         metaslab_group_remove(mg, msp);
649
650         mutex_enter(&msp->ms_lock);
651
652         space_map_unload(&msp->ms_map);
653         space_map_destroy(&msp->ms_map);
654
655         for (t = 0; t < TXG_SIZE; t++) {
656                 space_map_destroy(&msp->ms_allocmap[t]);
657                 space_map_destroy(&msp->ms_freemap[t]);
658         }
659
660         mutex_exit(&msp->ms_lock);
661         mutex_destroy(&msp->ms_lock);
662
663         kmem_free(msp, sizeof (metaslab_t));
664 }
665
666 #define METASLAB_WEIGHT_PRIMARY         (1ULL << 63)
667 #define METASLAB_WEIGHT_SECONDARY       (1ULL << 62)
668 #define METASLAB_ACTIVE_MASK            \
669         (METASLAB_WEIGHT_PRIMARY | METASLAB_WEIGHT_SECONDARY)
670
671 static uint64_t
672 metaslab_weight(metaslab_t *msp)
673 {
674         metaslab_group_t *mg = msp->ms_group;
675         space_map_t *sm = &msp->ms_map;
676         space_map_obj_t *smo = &msp->ms_smo;
677         vdev_t *vd = mg->mg_vd;
678         uint64_t weight, space;
679
680         ASSERT(MUTEX_HELD(&msp->ms_lock));
681
682         /*
683          * The baseline weight is the metaslab's free space.
684          */
685         space = sm->sm_size - smo->smo_alloc;
686         weight = space;
687
688         /*
689          * Modern disks have uniform bit density and constant angular velocity.
690          * Therefore, the outer recording zones are faster (higher bandwidth)
691          * than the inner zones by the ratio of outer to inner track diameter,
692          * which is typically around 2:1.  We account for this by assigning
693          * higher weight to lower metaslabs (multiplier ranging from 2x to 1x).
694          * In effect, this means that we'll select the metaslab with the most
695          * free bandwidth rather than simply the one with the most free space.
696          */
697         weight = 2 * weight -
698             ((sm->sm_start >> vd->vdev_ms_shift) * weight) / vd->vdev_ms_count;
699         ASSERT(weight >= space && weight <= 2 * space);
700
701         /*
702          * For locality, assign higher weight to metaslabs which have
703          * a lower offset than what we've already activated.
704          */
705         if (sm->sm_start <= mg->mg_bonus_area)
706                 weight *= (metaslab_smo_bonus_pct / 100);
707         ASSERT(weight >= space &&
708             weight <= 2 * (metaslab_smo_bonus_pct / 100) * space);
709
710         if (sm->sm_loaded && !sm->sm_ops->smop_fragmented(sm)) {
711                 /*
712                  * If this metaslab is one we're actively using, adjust its
713                  * weight to make it preferable to any inactive metaslab so
714                  * we'll polish it off.
715                  */
716                 weight |= (msp->ms_weight & METASLAB_ACTIVE_MASK);
717         }
718         return (weight);
719 }
720
721 static void
722 metaslab_prefetch(metaslab_group_t *mg)
723 {
724         spa_t *spa = mg->mg_vd->vdev_spa;
725         metaslab_t *msp;
726         avl_tree_t *t = &mg->mg_metaslab_tree;
727         int m;
728
729         mutex_enter(&mg->mg_lock);
730
731         /*
732          * Prefetch the next potential metaslabs
733          */
734         for (msp = avl_first(t), m = 0; msp; msp = AVL_NEXT(t, msp), m++) {
735                 space_map_t *sm = &msp->ms_map;
736                 space_map_obj_t *smo = &msp->ms_smo;
737
738                 /* If we have reached our prefetch limit then we're done */
739                 if (m >= metaslab_prefetch_limit)
740                         break;
741
742                 if (!sm->sm_loaded && smo->smo_object != 0) {
743                         mutex_exit(&mg->mg_lock);
744                         dmu_prefetch(spa->spa_meta_objset, smo->smo_object,
745                             0ULL, smo->smo_objsize);
746                         mutex_enter(&mg->mg_lock);
747                 }
748         }
749         mutex_exit(&mg->mg_lock);
750 }
751
752 static int
753 metaslab_activate(metaslab_t *msp, uint64_t activation_weight, uint64_t size)
754 {
755         metaslab_group_t *mg = msp->ms_group;
756         space_map_t *sm = &msp->ms_map;
757         space_map_ops_t *sm_ops = msp->ms_group->mg_class->mc_ops;
758
759         ASSERT(MUTEX_HELD(&msp->ms_lock));
760
761         if ((msp->ms_weight & METASLAB_ACTIVE_MASK) == 0) {
762                 int error = space_map_load(sm, sm_ops, SM_FREE, &msp->ms_smo,
763                     msp->ms_group->mg_vd->vdev_spa->spa_meta_objset);
764                 if (error) {
765                         metaslab_group_sort(msp->ms_group, msp, 0);
766                         return (error);
767                 }
768
769                 /*
770                  * Track the bonus area as we activate new metaslabs.
771                  */
772                 if (sm->sm_start > mg->mg_bonus_area) {
773                         mutex_enter(&mg->mg_lock);
774                         mg->mg_bonus_area = sm->sm_start;
775                         mutex_exit(&mg->mg_lock);
776                 }
777
778                 /*
779                  * If we were able to load the map then make sure
780                  * that this map is still able to satisfy our request.
781                  */
782                 if (msp->ms_weight < size)
783                         return (ENOSPC);
784
785                 metaslab_group_sort(msp->ms_group, msp,
786                     msp->ms_weight | activation_weight);
787         }
788         ASSERT(sm->sm_loaded);
789         ASSERT(msp->ms_weight & METASLAB_ACTIVE_MASK);
790
791         return (0);
792 }
793
794 static void
795 metaslab_passivate(metaslab_t *msp, uint64_t size)
796 {
797         /*
798          * If size < SPA_MINBLOCKSIZE, then we will not allocate from
799          * this metaslab again.  In that case, it had better be empty,
800          * or we would be leaving space on the table.
801          */
802         ASSERT(size >= SPA_MINBLOCKSIZE || msp->ms_map.sm_space == 0);
803         metaslab_group_sort(msp->ms_group, msp, MIN(msp->ms_weight, size));
804         ASSERT((msp->ms_weight & METASLAB_ACTIVE_MASK) == 0);
805 }
806
807 /*
808  * Write a metaslab to disk in the context of the specified transaction group.
809  */
810 void
811 metaslab_sync(metaslab_t *msp, uint64_t txg)
812 {
813         vdev_t *vd = msp->ms_group->mg_vd;
814         spa_t *spa = vd->vdev_spa;
815         objset_t *mos = spa->spa_meta_objset;
816         space_map_t *allocmap = &msp->ms_allocmap[txg & TXG_MASK];
817         space_map_t *freemap = &msp->ms_freemap[txg & TXG_MASK];
818         space_map_t *freed_map = &msp->ms_freemap[TXG_CLEAN(txg) & TXG_MASK];
819         space_map_t *sm = &msp->ms_map;
820         space_map_obj_t *smo = &msp->ms_smo_syncing;
821         dmu_buf_t *db;
822         dmu_tx_t *tx;
823         int t;
824
825         tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
826
827         /*
828          * The only state that can actually be changing concurrently with
829          * metaslab_sync() is the metaslab's ms_map.  No other thread can
830          * be modifying this txg's allocmap, freemap, freed_map, or smo.
831          * Therefore, we only hold ms_lock to satify space_map ASSERTs.
832          * We drop it whenever we call into the DMU, because the DMU
833          * can call down to us (e.g. via zio_free()) at any time.
834          */
835         mutex_enter(&msp->ms_lock);
836
837         if (smo->smo_object == 0) {
838                 ASSERT(smo->smo_objsize == 0);
839                 ASSERT(smo->smo_alloc == 0);
840                 mutex_exit(&msp->ms_lock);
841                 smo->smo_object = dmu_object_alloc(mos,
842                     DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
843                     DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
844                 ASSERT(smo->smo_object != 0);
845                 dmu_write(mos, vd->vdev_ms_array, sizeof (uint64_t) *
846                     (sm->sm_start >> vd->vdev_ms_shift),
847                     sizeof (uint64_t), &smo->smo_object, tx);
848                 mutex_enter(&msp->ms_lock);
849         }
850
851         space_map_walk(freemap, space_map_add, freed_map);
852
853         if (sm->sm_loaded && spa_sync_pass(spa) == 1 && smo->smo_objsize >=
854             2 * sizeof (uint64_t) * avl_numnodes(&sm->sm_root)) {
855                 /*
856                  * The in-core space map representation is twice as compact
857                  * as the on-disk one, so it's time to condense the latter
858                  * by generating a pure allocmap from first principles.
859                  *
860                  * This metaslab is 100% allocated,
861                  * minus the content of the in-core map (sm),
862                  * minus what's been freed this txg (freed_map),
863                  * minus allocations from txgs in the future
864                  * (because they haven't been committed yet).
865                  */
866                 space_map_vacate(allocmap, NULL, NULL);
867                 space_map_vacate(freemap, NULL, NULL);
868
869                 space_map_add(allocmap, allocmap->sm_start, allocmap->sm_size);
870
871                 space_map_walk(sm, space_map_remove, allocmap);
872                 space_map_walk(freed_map, space_map_remove, allocmap);
873
874                 for (t = 1; t < TXG_CONCURRENT_STATES; t++)
875                         space_map_walk(&msp->ms_allocmap[(txg + t) & TXG_MASK],
876                             space_map_remove, allocmap);
877
878                 mutex_exit(&msp->ms_lock);
879                 space_map_truncate(smo, mos, tx);
880                 mutex_enter(&msp->ms_lock);
881         }
882
883         space_map_sync(allocmap, SM_ALLOC, smo, mos, tx);
884         space_map_sync(freemap, SM_FREE, smo, mos, tx);
885
886         mutex_exit(&msp->ms_lock);
887
888         VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
889         dmu_buf_will_dirty(db, tx);
890         ASSERT3U(db->db_size, >=, sizeof (*smo));
891         bcopy(smo, db->db_data, sizeof (*smo));
892         dmu_buf_rele(db, FTAG);
893
894         dmu_tx_commit(tx);
895 }
896
897 /*
898  * Called after a transaction group has completely synced to mark
899  * all of the metaslab's free space as usable.
900  */
901 void
902 metaslab_sync_done(metaslab_t *msp, uint64_t txg)
903 {
904         space_map_obj_t *smo = &msp->ms_smo;
905         space_map_obj_t *smosync = &msp->ms_smo_syncing;
906         space_map_t *sm = &msp->ms_map;
907         space_map_t *freed_map = &msp->ms_freemap[TXG_CLEAN(txg) & TXG_MASK];
908         metaslab_group_t *mg = msp->ms_group;
909         vdev_t *vd = mg->mg_vd;
910         int t;
911
912         mutex_enter(&msp->ms_lock);
913
914         /*
915          * If this metaslab is just becoming available, initialize its
916          * allocmaps and freemaps and add its capacity to the vdev.
917          */
918         if (freed_map->sm_size == 0) {
919                 for (t = 0; t < TXG_SIZE; t++) {
920                         space_map_create(&msp->ms_allocmap[t], sm->sm_start,
921                             sm->sm_size, sm->sm_shift, sm->sm_lock);
922                         space_map_create(&msp->ms_freemap[t], sm->sm_start,
923                             sm->sm_size, sm->sm_shift, sm->sm_lock);
924                 }
925                 vdev_space_update(vd, sm->sm_size, 0, B_TRUE);
926         }
927
928         vdev_space_update(vd, 0, smosync->smo_alloc - smo->smo_alloc, B_TRUE);
929
930         ASSERT(msp->ms_allocmap[txg & TXG_MASK].sm_space == 0);
931         ASSERT(msp->ms_freemap[txg & TXG_MASK].sm_space == 0);
932
933         /*
934          * If there's a space_map_load() in progress, wait for it to complete
935          * so that we have a consistent view of the in-core space map.
936          * Then, add everything we freed in this txg to the map.
937          */
938         space_map_load_wait(sm);
939         space_map_vacate(freed_map, sm->sm_loaded ? space_map_free : NULL, sm);
940
941         *smo = *smosync;
942
943         /*
944          * If the map is loaded but no longer active, evict it as soon as all
945          * future allocations have synced.  (If we unloaded it now and then
946          * loaded a moment later, the map wouldn't reflect those allocations.)
947          */
948         if (sm->sm_loaded && (msp->ms_weight & METASLAB_ACTIVE_MASK) == 0) {
949                 int evictable = 1;
950
951                 for (t = 1; t < TXG_CONCURRENT_STATES; t++)
952                         if (msp->ms_allocmap[(txg + t) & TXG_MASK].sm_space)
953                                 evictable = 0;
954
955                 if (evictable)
956                         space_map_unload(sm);
957         }
958
959         metaslab_group_sort(mg, msp, metaslab_weight(msp));
960
961         mutex_exit(&msp->ms_lock);
962 }
963
964 void
965 metaslab_sync_reassess(metaslab_group_t *mg)
966 {
967         vdev_t *vd = mg->mg_vd;
968
969         /*
970          * Re-evaluate all metaslabs which have lower offsets than the
971          * bonus area.
972          */
973         for (int m = 0; m < vd->vdev_ms_count; m++) {
974                 metaslab_t *msp = vd->vdev_ms[m];
975
976                 if (msp->ms_map.sm_start > mg->mg_bonus_area)
977                         break;
978
979                 mutex_enter(&msp->ms_lock);
980                 metaslab_group_sort(mg, msp, metaslab_weight(msp));
981                 mutex_exit(&msp->ms_lock);
982         }
983
984         /*
985          * Prefetch the next potential metaslabs
986          */
987         metaslab_prefetch(mg);
988 }
989
990 static uint64_t
991 metaslab_distance(metaslab_t *msp, dva_t *dva)
992 {
993         uint64_t ms_shift = msp->ms_group->mg_vd->vdev_ms_shift;
994         uint64_t offset = DVA_GET_OFFSET(dva) >> ms_shift;
995         uint64_t start = msp->ms_map.sm_start >> ms_shift;
996
997         if (msp->ms_group->mg_vd->vdev_id != DVA_GET_VDEV(dva))
998                 return (1ULL << 63);
999
1000         if (offset < start)
1001                 return ((start - offset) << ms_shift);
1002         if (offset > start)
1003                 return ((offset - start) << ms_shift);
1004         return (0);
1005 }
1006
1007 static uint64_t
1008 metaslab_group_alloc(metaslab_group_t *mg, uint64_t size, uint64_t txg,
1009     uint64_t min_distance, dva_t *dva, int d)
1010 {
1011         metaslab_t *msp = NULL;
1012         uint64_t offset = -1ULL;
1013         avl_tree_t *t = &mg->mg_metaslab_tree;
1014         uint64_t activation_weight;
1015         uint64_t target_distance;
1016         int i;
1017
1018         activation_weight = METASLAB_WEIGHT_PRIMARY;
1019         for (i = 0; i < d; i++) {
1020                 if (DVA_GET_VDEV(&dva[i]) == mg->mg_vd->vdev_id) {
1021                         activation_weight = METASLAB_WEIGHT_SECONDARY;
1022                         break;
1023                 }
1024         }
1025
1026         for (;;) {
1027                 boolean_t was_active;
1028
1029                 mutex_enter(&mg->mg_lock);
1030                 for (msp = avl_first(t); msp; msp = AVL_NEXT(t, msp)) {
1031                         if (msp->ms_weight < size) {
1032                                 mutex_exit(&mg->mg_lock);
1033                                 return (-1ULL);
1034                         }
1035
1036                         was_active = msp->ms_weight & METASLAB_ACTIVE_MASK;
1037                         if (activation_weight == METASLAB_WEIGHT_PRIMARY)
1038                                 break;
1039
1040                         target_distance = min_distance +
1041                             (msp->ms_smo.smo_alloc ? 0 : min_distance >> 1);
1042
1043                         for (i = 0; i < d; i++)
1044                                 if (metaslab_distance(msp, &dva[i]) <
1045                                     target_distance)
1046                                         break;
1047                         if (i == d)
1048                                 break;
1049                 }
1050                 mutex_exit(&mg->mg_lock);
1051                 if (msp == NULL)
1052                         return (-1ULL);
1053
1054                 mutex_enter(&msp->ms_lock);
1055
1056                 /*
1057                  * Ensure that the metaslab we have selected is still
1058                  * capable of handling our request. It's possible that
1059                  * another thread may have changed the weight while we
1060                  * were blocked on the metaslab lock.
1061                  */
1062                 if (msp->ms_weight < size || (was_active &&
1063                     !(msp->ms_weight & METASLAB_ACTIVE_MASK) &&
1064                     activation_weight == METASLAB_WEIGHT_PRIMARY)) {
1065                         mutex_exit(&msp->ms_lock);
1066                         continue;
1067                 }
1068
1069                 if ((msp->ms_weight & METASLAB_WEIGHT_SECONDARY) &&
1070                     activation_weight == METASLAB_WEIGHT_PRIMARY) {
1071                         metaslab_passivate(msp,
1072                             msp->ms_weight & ~METASLAB_ACTIVE_MASK);
1073                         mutex_exit(&msp->ms_lock);
1074                         continue;
1075                 }
1076
1077                 if (metaslab_activate(msp, activation_weight, size) != 0) {
1078                         mutex_exit(&msp->ms_lock);
1079                         continue;
1080                 }
1081
1082                 if ((offset = space_map_alloc(&msp->ms_map, size)) != -1ULL)
1083                         break;
1084
1085                 metaslab_passivate(msp, space_map_maxsize(&msp->ms_map));
1086
1087                 mutex_exit(&msp->ms_lock);
1088         }
1089
1090         if (msp->ms_allocmap[txg & TXG_MASK].sm_space == 0)
1091                 vdev_dirty(mg->mg_vd, VDD_METASLAB, msp, txg);
1092
1093         space_map_add(&msp->ms_allocmap[txg & TXG_MASK], offset, size);
1094
1095         mutex_exit(&msp->ms_lock);
1096
1097         return (offset);
1098 }
1099
1100 /*
1101  * Allocate a block for the specified i/o.
1102  */
1103 static int
1104 metaslab_alloc_dva(spa_t *spa, metaslab_class_t *mc, uint64_t psize,
1105     dva_t *dva, int d, dva_t *hintdva, uint64_t txg, int flags)
1106 {
1107         metaslab_group_t *mg, *rotor;
1108         vdev_t *vd;
1109         int dshift = 3;
1110         int all_zero;
1111         int zio_lock = B_FALSE;
1112         boolean_t allocatable;
1113         uint64_t offset = -1ULL;
1114         uint64_t asize;
1115         uint64_t distance;
1116
1117         ASSERT(!DVA_IS_VALID(&dva[d]));
1118
1119         /*
1120          * For testing, make some blocks above a certain size be gang blocks.
1121          */
1122         if (psize >= metaslab_gang_bang && (LBOLT & 3) == 0)
1123                 return (ENOSPC);
1124
1125         /*
1126          * Start at the rotor and loop through all mgs until we find something.
1127          * Note that there's no locking on mc_rotor or mc_allocated because
1128          * nothing actually breaks if we miss a few updates -- we just won't
1129          * allocate quite as evenly.  It all balances out over time.
1130          *
1131          * If we are doing ditto or log blocks, try to spread them across
1132          * consecutive vdevs.  If we're forced to reuse a vdev before we've
1133          * allocated all of our ditto blocks, then try and spread them out on
1134          * that vdev as much as possible.  If it turns out to not be possible,
1135          * gradually lower our standards until anything becomes acceptable.
1136          * Also, allocating on consecutive vdevs (as opposed to random vdevs)
1137          * gives us hope of containing our fault domains to something we're
1138          * able to reason about.  Otherwise, any two top-level vdev failures
1139          * will guarantee the loss of data.  With consecutive allocation,
1140          * only two adjacent top-level vdev failures will result in data loss.
1141          *
1142          * If we are doing gang blocks (hintdva is non-NULL), try to keep
1143          * ourselves on the same vdev as our gang block header.  That
1144          * way, we can hope for locality in vdev_cache, plus it makes our
1145          * fault domains something tractable.
1146          */
1147         if (hintdva) {
1148                 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&hintdva[d]));
1149                 if (flags & METASLAB_HINTBP_AVOID)
1150                         mg = vd->vdev_mg->mg_next;
1151                 else
1152                         mg = vd->vdev_mg;
1153         } else if (d != 0) {
1154                 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d - 1]));
1155                 mg = vd->vdev_mg->mg_next;
1156         } else {
1157                 mg = mc->mc_rotor;
1158         }
1159
1160         /*
1161          * If the hint put us into the wrong class, just follow the rotor.
1162          */
1163         if (mg->mg_class != mc)
1164                 mg = mc->mc_rotor;
1165
1166         rotor = mg;
1167 top:
1168         all_zero = B_TRUE;
1169         do {
1170                 vd = mg->mg_vd;
1171
1172                 /*
1173                  * Don't allocate from faulted devices.
1174                  */
1175                 if (zio_lock) {
1176                         spa_config_enter(spa, SCL_ZIO, FTAG, RW_READER);
1177                         allocatable = vdev_allocatable(vd);
1178                         spa_config_exit(spa, SCL_ZIO, FTAG);
1179                 } else {
1180                         allocatable = vdev_allocatable(vd);
1181                 }
1182                 if (!allocatable)
1183                         goto next;
1184
1185                 /*
1186                  * Avoid writing single-copy data to a failing vdev
1187                  */
1188                 if ((vd->vdev_stat.vs_write_errors > 0 ||
1189                     vd->vdev_state < VDEV_STATE_HEALTHY) &&
1190                     d == 0 && dshift == 3) {
1191                         all_zero = B_FALSE;
1192                         goto next;
1193                 }
1194
1195                 ASSERT(mg->mg_class == mc);
1196
1197                 distance = vd->vdev_asize >> dshift;
1198                 if (distance <= (1ULL << vd->vdev_ms_shift))
1199                         distance = 0;
1200                 else
1201                         all_zero = B_FALSE;
1202
1203                 asize = vdev_psize_to_asize(vd, psize);
1204                 ASSERT(P2PHASE(asize, 1ULL << vd->vdev_ashift) == 0);
1205
1206                 offset = metaslab_group_alloc(mg, asize, txg, distance, dva, d);
1207                 if (offset != -1ULL) {
1208                         /*
1209                          * If we've just selected this metaslab group,
1210                          * figure out whether the corresponding vdev is
1211                          * over- or under-used relative to the pool,
1212                          * and set an allocation bias to even it out.
1213                          */
1214                         if (mc->mc_allocated == 0) {
1215                                 vdev_stat_t *vs = &vd->vdev_stat;
1216                                 uint64_t alloc, space;
1217                                 int64_t vu, su;
1218
1219                                 alloc = spa_get_alloc(spa);
1220                                 space = spa_get_space(spa);
1221
1222                                 /*
1223                                  * Determine percent used in units of 0..1024.
1224                                  * (This is just to avoid floating point.)
1225                                  */
1226                                 vu = (vs->vs_alloc << 10) / (vs->vs_space + 1);
1227                                 su = (alloc << 10) / (space + 1);
1228
1229                                 /*
1230                                  * Bias by at most +/- 25% of the aliquot.
1231                                  */
1232                                 mg->mg_bias = ((su - vu) *
1233                                     (int64_t)mg->mg_aliquot) / (1024 * 4);
1234                         }
1235
1236                         if (atomic_add_64_nv(&mc->mc_allocated, asize) >=
1237                             mg->mg_aliquot + mg->mg_bias) {
1238                                 mc->mc_rotor = mg->mg_next;
1239                                 mc->mc_allocated = 0;
1240                         }
1241
1242                         DVA_SET_VDEV(&dva[d], vd->vdev_id);
1243                         DVA_SET_OFFSET(&dva[d], offset);
1244                         DVA_SET_GANG(&dva[d], !!(flags & METASLAB_GANG_HEADER));
1245                         DVA_SET_ASIZE(&dva[d], asize);
1246
1247                         return (0);
1248                 }
1249 next:
1250                 mc->mc_rotor = mg->mg_next;
1251                 mc->mc_allocated = 0;
1252         } while ((mg = mg->mg_next) != rotor);
1253
1254         if (!all_zero) {
1255                 dshift++;
1256                 ASSERT(dshift < 64);
1257                 goto top;
1258         }
1259
1260         if (!allocatable && !zio_lock) {
1261                 dshift = 3;
1262                 zio_lock = B_TRUE;
1263                 goto top;
1264         }
1265
1266         bzero(&dva[d], sizeof (dva_t));
1267
1268         return (ENOSPC);
1269 }
1270
1271 /*
1272  * Free the block represented by DVA in the context of the specified
1273  * transaction group.
1274  */
1275 static void
1276 metaslab_free_dva(spa_t *spa, const dva_t *dva, uint64_t txg, boolean_t now)
1277 {
1278         uint64_t vdev = DVA_GET_VDEV(dva);
1279         uint64_t offset = DVA_GET_OFFSET(dva);
1280         uint64_t size = DVA_GET_ASIZE(dva);
1281         vdev_t *vd;
1282         metaslab_t *msp;
1283
1284         ASSERT(DVA_IS_VALID(dva));
1285
1286         if (txg > spa_freeze_txg(spa))
1287                 return;
1288
1289         if ((vd = vdev_lookup_top(spa, vdev)) == NULL ||
1290             (offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count) {
1291                 cmn_err(CE_WARN, "metaslab_free_dva(): bad DVA %llu:%llu",
1292                     (u_longlong_t)vdev, (u_longlong_t)offset);
1293                 ASSERT(0);
1294                 return;
1295         }
1296
1297         msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
1298
1299         if (DVA_GET_GANG(dva))
1300                 size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
1301
1302         mutex_enter(&msp->ms_lock);
1303
1304         if (now) {
1305                 space_map_remove(&msp->ms_allocmap[txg & TXG_MASK],
1306                     offset, size);
1307                 space_map_free(&msp->ms_map, offset, size);
1308         } else {
1309                 if (msp->ms_freemap[txg & TXG_MASK].sm_space == 0)
1310                         vdev_dirty(vd, VDD_METASLAB, msp, txg);
1311                 space_map_add(&msp->ms_freemap[txg & TXG_MASK], offset, size);
1312         }
1313
1314         mutex_exit(&msp->ms_lock);
1315 }
1316
1317 /*
1318  * Intent log support: upon opening the pool after a crash, notify the SPA
1319  * of blocks that the intent log has allocated for immediate write, but
1320  * which are still considered free by the SPA because the last transaction
1321  * group didn't commit yet.
1322  */
1323 static int
1324 metaslab_claim_dva(spa_t *spa, const dva_t *dva, uint64_t txg)
1325 {
1326         uint64_t vdev = DVA_GET_VDEV(dva);
1327         uint64_t offset = DVA_GET_OFFSET(dva);
1328         uint64_t size = DVA_GET_ASIZE(dva);
1329         vdev_t *vd;
1330         metaslab_t *msp;
1331         int error;
1332
1333         ASSERT(DVA_IS_VALID(dva));
1334
1335         if ((vd = vdev_lookup_top(spa, vdev)) == NULL ||
1336             (offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count)
1337                 return (ENXIO);
1338
1339         msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
1340
1341         if (DVA_GET_GANG(dva))
1342                 size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
1343
1344         mutex_enter(&msp->ms_lock);
1345
1346         error = metaslab_activate(msp, METASLAB_WEIGHT_SECONDARY, 0);
1347         if (error || txg == 0) {        /* txg == 0 indicates dry run */
1348                 mutex_exit(&msp->ms_lock);
1349                 return (error);
1350         }
1351
1352         space_map_claim(&msp->ms_map, offset, size);
1353
1354         if (spa_writeable(spa)) {       /* don't dirty if we're zdb(1M) */
1355                 if (msp->ms_allocmap[txg & TXG_MASK].sm_space == 0)
1356                         vdev_dirty(vd, VDD_METASLAB, msp, txg);
1357                 space_map_add(&msp->ms_allocmap[txg & TXG_MASK], offset, size);
1358         }
1359
1360         mutex_exit(&msp->ms_lock);
1361
1362         return (0);
1363 }
1364
1365 int
1366 metaslab_alloc(spa_t *spa, metaslab_class_t *mc, uint64_t psize, blkptr_t *bp,
1367     int ndvas, uint64_t txg, blkptr_t *hintbp, int flags)
1368 {
1369         dva_t *dva = bp->blk_dva;
1370         dva_t *hintdva = hintbp->blk_dva;
1371         int error = 0;
1372
1373         ASSERT(bp->blk_birth == 0);
1374
1375         spa_config_enter(spa, SCL_ALLOC, FTAG, RW_READER);
1376
1377         if (mc->mc_rotor == NULL) {     /* no vdevs in this class */
1378                 spa_config_exit(spa, SCL_ALLOC, FTAG);
1379                 return (ENOSPC);
1380         }
1381
1382         ASSERT(ndvas > 0 && ndvas <= spa_max_replication(spa));
1383         ASSERT(BP_GET_NDVAS(bp) == 0);
1384         ASSERT(hintbp == NULL || ndvas <= BP_GET_NDVAS(hintbp));
1385
1386         for (int d = 0; d < ndvas; d++) {
1387                 error = metaslab_alloc_dva(spa, mc, psize, dva, d, hintdva,
1388                     txg, flags);
1389                 if (error) {
1390                         for (d--; d >= 0; d--) {
1391                                 metaslab_free_dva(spa, &dva[d], txg, B_TRUE);
1392                                 bzero(&dva[d], sizeof (dva_t));
1393                         }
1394                         spa_config_exit(spa, SCL_ALLOC, FTAG);
1395                         return (error);
1396                 }
1397         }
1398         ASSERT(error == 0);
1399         ASSERT(BP_GET_NDVAS(bp) == ndvas);
1400
1401         spa_config_exit(spa, SCL_ALLOC, FTAG);
1402
1403         bp->blk_birth = txg;
1404
1405         return (0);
1406 }
1407
1408 void
1409 metaslab_free(spa_t *spa, const blkptr_t *bp, uint64_t txg, boolean_t now)
1410 {
1411         const dva_t *dva = bp->blk_dva;
1412         int ndvas = BP_GET_NDVAS(bp);
1413
1414         ASSERT(!BP_IS_HOLE(bp));
1415         ASSERT(!now || bp->blk_birth >= spa->spa_syncing_txg);
1416
1417         spa_config_enter(spa, SCL_FREE, FTAG, RW_READER);
1418
1419         for (int d = 0; d < ndvas; d++)
1420                 metaslab_free_dva(spa, &dva[d], txg, now);
1421
1422         spa_config_exit(spa, SCL_FREE, FTAG);
1423 }
1424
1425 int
1426 metaslab_claim(spa_t *spa, const blkptr_t *bp, uint64_t txg)
1427 {
1428         const dva_t *dva = bp->blk_dva;
1429         int ndvas = BP_GET_NDVAS(bp);
1430         int error = 0;
1431
1432         ASSERT(!BP_IS_HOLE(bp));
1433
1434         if (txg != 0) {
1435                 /*
1436                  * First do a dry run to make sure all DVAs are claimable,
1437                  * so we don't have to unwind from partial failures below.
1438                  */
1439                 if ((error = metaslab_claim(spa, bp, 0)) != 0)
1440                         return (error);
1441         }
1442
1443         spa_config_enter(spa, SCL_ALLOC, FTAG, RW_READER);
1444
1445         for (int d = 0; d < ndvas; d++)
1446                 if ((error = metaslab_claim_dva(spa, &dva[d], txg)) != 0)
1447                         break;
1448
1449         spa_config_exit(spa, SCL_ALLOC, FTAG);
1450
1451         ASSERT(error == 0 || txg == 0);
1452
1453         return (error);
1454 }