metaslab_impl.h revision 262093
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) 2013 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
41extern "C" {
42#endif
43
44struct metaslab_class {
45	spa_t			*mc_spa;
46	metaslab_group_t	*mc_rotor;
47	metaslab_ops_t		*mc_ops;
48	uint64_t		mc_aliquot;
49	uint64_t		mc_alloc_groups; /* # of allocatable groups */
50	uint64_t		mc_alloc;	/* total allocated space */
51	uint64_t		mc_deferred;	/* total deferred frees */
52	uint64_t		mc_space;	/* total space (alloc + free) */
53	uint64_t		mc_dspace;	/* total deflated space */
54	uint64_t		mc_minblocksize;
55};
56
57struct metaslab_group {
58	kmutex_t		mg_lock;
59	avl_tree_t		mg_metaslab_tree;
60	uint64_t		mg_aliquot;
61	uint64_t		mg_alloc_failures;
62	boolean_t		mg_allocatable;		/* can we allocate? */
63	uint64_t		mg_free_capacity;	/* percentage free */
64	int64_t			mg_bias;
65	int64_t			mg_activation_count;
66	metaslab_class_t	*mg_class;
67	vdev_t			*mg_vd;
68	taskq_t			*mg_taskq;
69	metaslab_group_t	*mg_prev;
70	metaslab_group_t	*mg_next;
71};
72
73/*
74 * This value defines the number of elements in the ms_lbas array. The value
75 * of 64 was chosen as it covers to cover all power of 2 buckets up to
76 * UINT64_MAX. This is the equivalent of highbit(UINT64_MAX).
77 */
78#define	MAX_LBAS	64
79
80/*
81 * Each metaslab maintains a set of in-core trees to track metaslab operations.
82 * The in-core free tree (ms_tree) contains the current list of free segments.
83 * As blocks are allocated, the allocated segment are removed from the ms_tree
84 * and added to a per txg allocation tree (ms_alloctree). As blocks are freed,
85 * they are added to the per txg free tree (ms_freetree). These per txg
86 * trees allow us to process all allocations and frees in syncing context
87 * where it is safe to update the on-disk space maps. One additional in-core
88 * tree is maintained to track deferred frees (ms_defertree). Once a block
89 * is freed it will move from the ms_freetree to the ms_defertree. A deferred
90 * free means that a block has been freed but cannot be used by the pool
91 * until TXG_DEFER_SIZE transactions groups later. For example, a block
92 * that is freed in txg 50 will not be available for reallocation until
93 * txg 52 (50 + TXG_DEFER_SIZE).  This provides a safety net for uberblock
94 * rollback. A pool could be safely rolled back TXG_DEFERS_SIZE
95 * transactions groups and ensure that no block has been reallocated.
96 *
97 * The simplified transition diagram looks like this:
98 *
99 *
100 *      ALLOCATE
101 *         |
102 *         V
103 *    free segment (ms_tree) --------> ms_alloctree ----> (write to space map)
104 *         ^
105 *         |
106 *         |                           ms_freetree <--- FREE
107 *         |                                 |
108 *         |                                 |
109 *         |                                 |
110 *         +----------- ms_defertree <-------+---------> (write to space map)
111 *
112 *
113 * Each metaslab's space is tracked in a single space map in the MOS,
114 * which is only updated in syncing context. Each time we sync a txg,
115 * we append the allocs and frees from that txg to the space map.
116 * The pool space is only updated once all metaslabs have finished syncing.
117 *
118 * To load the in-core free tree we read the space map from disk.
119 * This object contains a series of alloc and free records that are
120 * combined to make up the list of all free segments in this metaslab. These
121 * segments are represented in-core by the ms_tree and are stored in an
122 * AVL tree.
123 *
124 * As the space map grows (as a result of the appends) it will
125 * eventually become space-inefficient. When the metaslab's in-core free tree
126 * is zfs_condense_pct/100 times the size of the minimal on-disk
127 * representation, we rewrite it in its minimized form. If a metaslab
128 * needs to condense then we must set the ms_condensing flag to ensure
129 * that allocations are not performed on the metaslab that is being written.
130 */
131struct metaslab {
132	kmutex_t	ms_lock;
133	kcondvar_t	ms_load_cv;
134	space_map_t	*ms_sm;
135	metaslab_ops_t	*ms_ops;
136	uint64_t	ms_id;
137	uint64_t	ms_start;
138	uint64_t	ms_size;
139
140	range_tree_t	*ms_alloctree[TXG_SIZE];
141	range_tree_t	*ms_freetree[TXG_SIZE];
142	range_tree_t	*ms_defertree[TXG_DEFER_SIZE];
143	range_tree_t	*ms_tree;
144
145	boolean_t	ms_condensing;	/* condensing? */
146	boolean_t	ms_loaded;
147	boolean_t	ms_loading;
148
149	int64_t		ms_deferspace;	/* sum of ms_defermap[] space	*/
150	uint64_t	ms_weight;	/* weight vs. others in group	*/
151	uint64_t	ms_factor;
152	uint64_t	ms_access_txg;
153
154	/*
155	 * The metaslab block allocators can optionally use a size-ordered
156	 * range tree and/or an array of LBAs. Not all allocators use
157	 * this functionality. The ms_size_tree should always contain the
158	 * same number of segments as the ms_tree. The only difference
159	 * is that the ms_size_tree is ordered by segment sizes.
160	 */
161	avl_tree_t	ms_size_tree;
162	uint64_t	ms_lbas[MAX_LBAS];
163
164	metaslab_group_t *ms_group;	/* metaslab group		*/
165	avl_node_t	ms_group_node;	/* node in metaslab group tree	*/
166	txg_node_t	ms_txg_node;	/* per-txg dirty metaslab links	*/
167};
168
169#ifdef	__cplusplus
170}
171#endif
172
173#endif	/* _SYS_METASLAB_IMPL_H */
174