xref: /titanic_41/usr/src/uts/common/fs/zfs/sys/metaslab_impl.h (revision 02b647c5b841d8d71800e564d031166e277e97ef)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
28  */
29 
30 #ifndef _SYS_METASLAB_IMPL_H
31 #define	_SYS_METASLAB_IMPL_H
32 
33 #include <sys/metaslab.h>
34 #include <sys/space_map.h>
35 #include <sys/range_tree.h>
36 #include <sys/vdev.h>
37 #include <sys/txg.h>
38 #include <sys/avl.h>
39 
40 #ifdef	__cplusplus
41 extern "C" {
42 #endif
43 
44 /*
45  * A metaslab class encompasses a category of allocatable top-level vdevs.
46  * Each top-level vdev is associated with a metaslab group which defines
47  * the allocatable region for that vdev. Examples of these categories include
48  * "normal" for data block allocations (i.e. main pool allocations) or "log"
49  * for allocations designated for intent log devices (i.e. slog devices).
50  * When a block allocation is requested from the SPA it is associated with a
51  * metaslab_class_t, and only top-level vdevs (i.e. metaslab groups) belonging
52  * to the class can be used to satisfy that request. Allocations are done
53  * by traversing the metaslab groups that are linked off of the mc_rotor field.
54  * This rotor points to the next metaslab group where allocations will be
55  * attempted. Allocating a block is a 3 step process -- select the metaslab
56  * group, select the metaslab, and then allocate the block. The metaslab
57  * class defines the low-level block allocator that will be used as the
58  * final step in allocation. These allocators are pluggable allowing each class
59  * to use a block allocator that best suits that class.
60  */
61 struct metaslab_class {
62 	spa_t			*mc_spa;
63 	metaslab_group_t	*mc_rotor;
64 	metaslab_ops_t		*mc_ops;
65 	uint64_t		mc_aliquot;
66 	uint64_t		mc_alloc_groups; /* # of allocatable groups */
67 	uint64_t		mc_alloc;	/* total allocated space */
68 	uint64_t		mc_deferred;	/* total deferred frees */
69 	uint64_t		mc_space;	/* total space (alloc + free) */
70 	uint64_t		mc_dspace;	/* total deflated space */
71 	uint64_t		mc_histogram[RANGE_TREE_HISTOGRAM_SIZE];
72 };
73 
74 /*
75  * Metaslab groups encapsulate all the allocatable regions (i.e. metaslabs)
76  * of a top-level vdev. They are linked togther to form a circular linked
77  * list and can belong to only one metaslab class. Metaslab groups may become
78  * ineligible for allocations for a number of reasons such as limited free
79  * space, fragmentation, or going offline. When this happens the allocator will
80  * simply find the next metaslab group in the linked list and attempt
81  * to allocate from that group instead.
82  */
83 struct metaslab_group {
84 	kmutex_t		mg_lock;
85 	avl_tree_t		mg_metaslab_tree;
86 	uint64_t		mg_aliquot;
87 	boolean_t		mg_allocatable;		/* can we allocate? */
88 	uint64_t		mg_free_capacity;	/* percentage free */
89 	int64_t			mg_bias;
90 	int64_t			mg_activation_count;
91 	metaslab_class_t	*mg_class;
92 	vdev_t			*mg_vd;
93 	taskq_t			*mg_taskq;
94 	metaslab_group_t	*mg_prev;
95 	metaslab_group_t	*mg_next;
96 	uint64_t		mg_fragmentation;
97 	uint64_t		mg_histogram[RANGE_TREE_HISTOGRAM_SIZE];
98 };
99 
100 /*
101  * This value defines the number of elements in the ms_lbas array. The value
102  * of 64 was chosen as it covers all power of 2 buckets up to UINT64_MAX.
103  * This is the equivalent of highbit(UINT64_MAX).
104  */
105 #define	MAX_LBAS	64
106 
107 /*
108  * Each metaslab maintains a set of in-core trees to track metaslab operations.
109  * The in-core free tree (ms_tree) contains the current list of free segments.
110  * As blocks are allocated, the allocated segment are removed from the ms_tree
111  * and added to a per txg allocation tree (ms_alloctree). As blocks are freed,
112  * they are added to the per txg free tree (ms_freetree). These per txg
113  * trees allow us to process all allocations and frees in syncing context
114  * where it is safe to update the on-disk space maps. One additional in-core
115  * tree is maintained to track deferred frees (ms_defertree). Once a block
116  * is freed it will move from the ms_freetree to the ms_defertree. A deferred
117  * free means that a block has been freed but cannot be used by the pool
118  * until TXG_DEFER_SIZE transactions groups later. For example, a block
119  * that is freed in txg 50 will not be available for reallocation until
120  * txg 52 (50 + TXG_DEFER_SIZE).  This provides a safety net for uberblock
121  * rollback. A pool could be safely rolled back TXG_DEFERS_SIZE
122  * transactions groups and ensure that no block has been reallocated.
123  *
124  * The simplified transition diagram looks like this:
125  *
126  *
127  *      ALLOCATE
128  *         |
129  *         V
130  *    free segment (ms_tree) --------> ms_alloctree ----> (write to space map)
131  *         ^
132  *         |
133  *         |                           ms_freetree <--- FREE
134  *         |                                 |
135  *         |                                 |
136  *         |                                 |
137  *         +----------- ms_defertree <-------+---------> (write to space map)
138  *
139  *
140  * Each metaslab's space is tracked in a single space map in the MOS,
141  * which is only updated in syncing context. Each time we sync a txg,
142  * we append the allocs and frees from that txg to the space map.
143  * The pool space is only updated once all metaslabs have finished syncing.
144  *
145  * To load the in-core free tree we read the space map from disk.
146  * This object contains a series of alloc and free records that are
147  * combined to make up the list of all free segments in this metaslab. These
148  * segments are represented in-core by the ms_tree and are stored in an
149  * AVL tree.
150  *
151  * As the space map grows (as a result of the appends) it will
152  * eventually become space-inefficient. When the metaslab's in-core free tree
153  * is zfs_condense_pct/100 times the size of the minimal on-disk
154  * representation, we rewrite it in its minimized form. If a metaslab
155  * needs to condense then we must set the ms_condensing flag to ensure
156  * that allocations are not performed on the metaslab that is being written.
157  */
158 struct metaslab {
159 	kmutex_t	ms_lock;
160 	kcondvar_t	ms_load_cv;
161 	space_map_t	*ms_sm;
162 	metaslab_ops_t	*ms_ops;
163 	uint64_t	ms_id;
164 	uint64_t	ms_start;
165 	uint64_t	ms_size;
166 	uint64_t	ms_fragmentation;
167 
168 	range_tree_t	*ms_alloctree[TXG_SIZE];
169 	range_tree_t	*ms_freetree[TXG_SIZE];
170 	range_tree_t	*ms_defertree[TXG_DEFER_SIZE];
171 	range_tree_t	*ms_tree;
172 
173 	boolean_t	ms_condensing;	/* condensing? */
174 	boolean_t	ms_condense_wanted;
175 	boolean_t	ms_loaded;
176 	boolean_t	ms_loading;
177 
178 	int64_t		ms_deferspace;	/* sum of ms_defermap[] space	*/
179 	uint64_t	ms_weight;	/* weight vs. others in group	*/
180 	uint64_t	ms_access_txg;
181 
182 	/*
183 	 * The metaslab block allocators can optionally use a size-ordered
184 	 * range tree and/or an array of LBAs. Not all allocators use
185 	 * this functionality. The ms_size_tree should always contain the
186 	 * same number of segments as the ms_tree. The only difference
187 	 * is that the ms_size_tree is ordered by segment sizes.
188 	 */
189 	avl_tree_t	ms_size_tree;
190 	uint64_t	ms_lbas[MAX_LBAS];
191 
192 	metaslab_group_t *ms_group;	/* metaslab group		*/
193 	avl_node_t	ms_group_node;	/* node in metaslab group tree	*/
194 	txg_node_t	ms_txg_node;	/* per-txg dirty metaslab links	*/
195 };
196 
197 #ifdef	__cplusplus
198 }
199 #endif
200 
201 #endif	/* _SYS_METASLAB_IMPL_H */
202