xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/metaslab_impl.h (revision 0f7643c7376dd69a08acbfc9d1d7d548b10c846a)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ecc2d604Sbonwick  * Common Development and Distribution License (the "License").
6ecc2d604Sbonwick  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22d6e555bdSGeorge Wilson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
2401f55e48SGeorge Wilson  */
2501f55e48SGeorge Wilson 
2601f55e48SGeorge Wilson /*
27*0f7643c7SGeorge Wilson  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
28fa9e4066Sahrens  */
29fa9e4066Sahrens 
30fa9e4066Sahrens #ifndef _SYS_METASLAB_IMPL_H
31fa9e4066Sahrens #define	_SYS_METASLAB_IMPL_H
32fa9e4066Sahrens 
33fa9e4066Sahrens #include <sys/metaslab.h>
34fa9e4066Sahrens #include <sys/space_map.h>
350713e232SGeorge Wilson #include <sys/range_tree.h>
36fa9e4066Sahrens #include <sys/vdev.h>
37fa9e4066Sahrens #include <sys/txg.h>
38fa9e4066Sahrens #include <sys/avl.h>
39fa9e4066Sahrens 
40fa9e4066Sahrens #ifdef	__cplusplus
41fa9e4066Sahrens extern "C" {
42fa9e4066Sahrens #endif
43fa9e4066Sahrens 
442e4c9986SGeorge Wilson /*
452e4c9986SGeorge Wilson  * A metaslab class encompasses a category of allocatable top-level vdevs.
462e4c9986SGeorge Wilson  * Each top-level vdev is associated with a metaslab group which defines
472e4c9986SGeorge Wilson  * the allocatable region for that vdev. Examples of these categories include
482e4c9986SGeorge Wilson  * "normal" for data block allocations (i.e. main pool allocations) or "log"
492e4c9986SGeorge Wilson  * for allocations designated for intent log devices (i.e. slog devices).
502e4c9986SGeorge Wilson  * When a block allocation is requested from the SPA it is associated with a
512e4c9986SGeorge Wilson  * metaslab_class_t, and only top-level vdevs (i.e. metaslab groups) belonging
522e4c9986SGeorge Wilson  * to the class can be used to satisfy that request. Allocations are done
532e4c9986SGeorge Wilson  * by traversing the metaslab groups that are linked off of the mc_rotor field.
542e4c9986SGeorge Wilson  * This rotor points to the next metaslab group where allocations will be
552e4c9986SGeorge Wilson  * attempted. Allocating a block is a 3 step process -- select the metaslab
562e4c9986SGeorge Wilson  * group, select the metaslab, and then allocate the block. The metaslab
572e4c9986SGeorge Wilson  * class defines the low-level block allocator that will be used as the
582e4c9986SGeorge Wilson  * final step in allocation. These allocators are pluggable allowing each class
592e4c9986SGeorge Wilson  * to use a block allocator that best suits that class.
602e4c9986SGeorge Wilson  */
61fa9e4066Sahrens struct metaslab_class {
62*0f7643c7SGeorge Wilson 	kmutex_t		mc_lock;
6388ecc943SGeorge Wilson 	spa_t			*mc_spa;
64fa9e4066Sahrens 	metaslab_group_t	*mc_rotor;
650713e232SGeorge Wilson 	metaslab_ops_t		*mc_ops;
66b24ab676SJeff Bonwick 	uint64_t		mc_aliquot;
67*0f7643c7SGeorge Wilson 
68*0f7643c7SGeorge Wilson 	/*
69*0f7643c7SGeorge Wilson 	 * Track the number of metaslab groups that have been initialized
70*0f7643c7SGeorge Wilson 	 * and can accept allocations. An initialized metaslab group is
71*0f7643c7SGeorge Wilson 	 * one has been completely added to the config (i.e. we have
72*0f7643c7SGeorge Wilson 	 * updated the MOS config and the space has been added to the pool).
73*0f7643c7SGeorge Wilson 	 */
74*0f7643c7SGeorge Wilson 	uint64_t		mc_groups;
75*0f7643c7SGeorge Wilson 
76*0f7643c7SGeorge Wilson 	/*
77*0f7643c7SGeorge Wilson 	 * Toggle to enable/disable the allocation throttle.
78*0f7643c7SGeorge Wilson 	 */
79*0f7643c7SGeorge Wilson 	boolean_t		mc_alloc_throttle_enabled;
80*0f7643c7SGeorge Wilson 
81*0f7643c7SGeorge Wilson 	/*
82*0f7643c7SGeorge Wilson 	 * The allocation throttle works on a reservation system. Whenever
83*0f7643c7SGeorge Wilson 	 * an asynchronous zio wants to perform an allocation it must
84*0f7643c7SGeorge Wilson 	 * first reserve the number of blocks that it wants to allocate.
85*0f7643c7SGeorge Wilson 	 * If there aren't sufficient slots available for the pending zio
86*0f7643c7SGeorge Wilson 	 * then that I/O is throttled until more slots free up. The current
87*0f7643c7SGeorge Wilson 	 * number of reserved allocations is maintained by the mc_alloc_slots
88*0f7643c7SGeorge Wilson 	 * refcount. The mc_alloc_max_slots value determines the maximum
89*0f7643c7SGeorge Wilson 	 * number of allocations that the system allows. Gang blocks are
90*0f7643c7SGeorge Wilson 	 * allowed to reserve slots even if we've reached the maximum
91*0f7643c7SGeorge Wilson 	 * number of allocations allowed.
92*0f7643c7SGeorge Wilson 	 */
93*0f7643c7SGeorge Wilson 	uint64_t		mc_alloc_max_slots;
94*0f7643c7SGeorge Wilson 	refcount_t		mc_alloc_slots;
95*0f7643c7SGeorge Wilson 
9622e30981SGeorge Wilson 	uint64_t		mc_alloc_groups; /* # of allocatable groups */
97*0f7643c7SGeorge Wilson 
98b24ab676SJeff Bonwick 	uint64_t		mc_alloc;	/* total allocated space */
99b24ab676SJeff Bonwick 	uint64_t		mc_deferred;	/* total deferred frees */
100b24ab676SJeff Bonwick 	uint64_t		mc_space;	/* total space (alloc + free) */
101b24ab676SJeff Bonwick 	uint64_t		mc_dspace;	/* total deflated space */
1022e4c9986SGeorge Wilson 	uint64_t		mc_histogram[RANGE_TREE_HISTOGRAM_SIZE];
103fa9e4066Sahrens };
104fa9e4066Sahrens 
1052e4c9986SGeorge Wilson /*
1062e4c9986SGeorge Wilson  * Metaslab groups encapsulate all the allocatable regions (i.e. metaslabs)
1072e4c9986SGeorge Wilson  * of a top-level vdev. They are linked togther to form a circular linked
1082e4c9986SGeorge Wilson  * list and can belong to only one metaslab class. Metaslab groups may become
1092e4c9986SGeorge Wilson  * ineligible for allocations for a number of reasons such as limited free
1102e4c9986SGeorge Wilson  * space, fragmentation, or going offline. When this happens the allocator will
1112e4c9986SGeorge Wilson  * simply find the next metaslab group in the linked list and attempt
1122e4c9986SGeorge Wilson  * to allocate from that group instead.
1132e4c9986SGeorge Wilson  */
114fa9e4066Sahrens struct metaslab_group {
115fa9e4066Sahrens 	kmutex_t		mg_lock;
116fa9e4066Sahrens 	avl_tree_t		mg_metaslab_tree;
117fa9e4066Sahrens 	uint64_t		mg_aliquot;
11822e30981SGeorge Wilson 	boolean_t		mg_allocatable;		/* can we allocate? */
119*0f7643c7SGeorge Wilson 
120*0f7643c7SGeorge Wilson 	/*
121*0f7643c7SGeorge Wilson 	 * A metaslab group is considered to be initialized only after
122*0f7643c7SGeorge Wilson 	 * we have updated the MOS config and added the space to the pool.
123*0f7643c7SGeorge Wilson 	 * We only allow allocation attempts to a metaslab group if it
124*0f7643c7SGeorge Wilson 	 * has been initialized.
125*0f7643c7SGeorge Wilson 	 */
126*0f7643c7SGeorge Wilson 	boolean_t		mg_initialized;
127*0f7643c7SGeorge Wilson 
12822e30981SGeorge Wilson 	uint64_t		mg_free_capacity;	/* percentage free */
129fa9e4066Sahrens 	int64_t			mg_bias;
130a1521560SJeff Bonwick 	int64_t			mg_activation_count;
131fa9e4066Sahrens 	metaslab_class_t	*mg_class;
132fa9e4066Sahrens 	vdev_t			*mg_vd;
1330713e232SGeorge Wilson 	taskq_t			*mg_taskq;
134fa9e4066Sahrens 	metaslab_group_t	*mg_prev;
135fa9e4066Sahrens 	metaslab_group_t	*mg_next;
136*0f7643c7SGeorge Wilson 
137*0f7643c7SGeorge Wilson 	/*
138*0f7643c7SGeorge Wilson 	 * Each metaslab group can handle mg_max_alloc_queue_depth allocations
139*0f7643c7SGeorge Wilson 	 * which are tracked by mg_alloc_queue_depth. It's possible for a
140*0f7643c7SGeorge Wilson 	 * metaslab group to handle more allocations than its max. This
141*0f7643c7SGeorge Wilson 	 * can occur when gang blocks are required or when other groups
142*0f7643c7SGeorge Wilson 	 * are unable to handle their share of allocations.
143*0f7643c7SGeorge Wilson 	 */
144*0f7643c7SGeorge Wilson 	uint64_t		mg_max_alloc_queue_depth;
145*0f7643c7SGeorge Wilson 	refcount_t		mg_alloc_queue_depth;
146*0f7643c7SGeorge Wilson 
147*0f7643c7SGeorge Wilson 	/*
148*0f7643c7SGeorge Wilson 	 * A metalab group that can no longer allocate the minimum block
149*0f7643c7SGeorge Wilson 	 * size will set mg_no_free_space. Once a metaslab group is out
150*0f7643c7SGeorge Wilson 	 * of space then its share of work must be distributed to other
151*0f7643c7SGeorge Wilson 	 * groups.
152*0f7643c7SGeorge Wilson 	 */
153*0f7643c7SGeorge Wilson 	boolean_t		mg_no_free_space;
154*0f7643c7SGeorge Wilson 
155*0f7643c7SGeorge Wilson 	uint64_t		mg_allocations;
156*0f7643c7SGeorge Wilson 	uint64_t		mg_failed_allocations;
1572e4c9986SGeorge Wilson 	uint64_t		mg_fragmentation;
1582e4c9986SGeorge Wilson 	uint64_t		mg_histogram[RANGE_TREE_HISTOGRAM_SIZE];
159fa9e4066Sahrens };
160fa9e4066Sahrens 
161fa9e4066Sahrens /*
1620713e232SGeorge Wilson  * This value defines the number of elements in the ms_lbas array. The value
1632e4c9986SGeorge Wilson  * of 64 was chosen as it covers all power of 2 buckets up to UINT64_MAX.
1642e4c9986SGeorge Wilson  * This is the equivalent of highbit(UINT64_MAX).
1650713e232SGeorge Wilson  */
1660713e232SGeorge Wilson #define	MAX_LBAS	64
1670713e232SGeorge Wilson 
1680713e232SGeorge Wilson /*
1690713e232SGeorge Wilson  * Each metaslab maintains a set of in-core trees to track metaslab operations.
1700713e232SGeorge Wilson  * The in-core free tree (ms_tree) contains the current list of free segments.
1710713e232SGeorge Wilson  * As blocks are allocated, the allocated segment are removed from the ms_tree
1720713e232SGeorge Wilson  * and added to a per txg allocation tree (ms_alloctree). As blocks are freed,
1730713e232SGeorge Wilson  * they are added to the per txg free tree (ms_freetree). These per txg
1740713e232SGeorge Wilson  * trees allow us to process all allocations and frees in syncing context
1750713e232SGeorge Wilson  * where it is safe to update the on-disk space maps. One additional in-core
1760713e232SGeorge Wilson  * tree is maintained to track deferred frees (ms_defertree). Once a block
1770713e232SGeorge Wilson  * is freed it will move from the ms_freetree to the ms_defertree. A deferred
1780713e232SGeorge Wilson  * free means that a block has been freed but cannot be used by the pool
1790713e232SGeorge Wilson  * until TXG_DEFER_SIZE transactions groups later. For example, a block
1800713e232SGeorge Wilson  * that is freed in txg 50 will not be available for reallocation until
1810713e232SGeorge Wilson  * txg 52 (50 + TXG_DEFER_SIZE).  This provides a safety net for uberblock
1820713e232SGeorge Wilson  * rollback. A pool could be safely rolled back TXG_DEFERS_SIZE
1830713e232SGeorge Wilson  * transactions groups and ensure that no block has been reallocated.
1840713e232SGeorge Wilson  *
1850713e232SGeorge Wilson  * The simplified transition diagram looks like this:
1860713e232SGeorge Wilson  *
1870713e232SGeorge Wilson  *
1880713e232SGeorge Wilson  *      ALLOCATE
1890713e232SGeorge Wilson  *         |
1900713e232SGeorge Wilson  *         V
1910713e232SGeorge Wilson  *    free segment (ms_tree) --------> ms_alloctree ----> (write to space map)
1920713e232SGeorge Wilson  *         ^
1930713e232SGeorge Wilson  *         |
1940713e232SGeorge Wilson  *         |                           ms_freetree <--- FREE
1950713e232SGeorge Wilson  *         |                                 |
1960713e232SGeorge Wilson  *         |                                 |
1970713e232SGeorge Wilson  *         |                                 |
1980713e232SGeorge Wilson  *         +----------- ms_defertree <-------+---------> (write to space map)
19916a4a807SGeorge Wilson  *
2000713e232SGeorge Wilson  *
2010713e232SGeorge Wilson  * Each metaslab's space is tracked in a single space map in the MOS,
20216a4a807SGeorge Wilson  * which is only updated in syncing context. Each time we sync a txg,
2030713e232SGeorge Wilson  * we append the allocs and frees from that txg to the space map.
2040713e232SGeorge Wilson  * The pool space is only updated once all metaslabs have finished syncing.
20516a4a807SGeorge Wilson  *
2060713e232SGeorge Wilson  * To load the in-core free tree we read the space map from disk.
20716a4a807SGeorge Wilson  * This object contains a series of alloc and free records that are
20816a4a807SGeorge Wilson  * combined to make up the list of all free segments in this metaslab. These
2090713e232SGeorge Wilson  * segments are represented in-core by the ms_tree and are stored in an
21016a4a807SGeorge Wilson  * AVL tree.
21116a4a807SGeorge Wilson  *
2120713e232SGeorge Wilson  * As the space map grows (as a result of the appends) it will
2130713e232SGeorge Wilson  * eventually become space-inefficient. When the metaslab's in-core free tree
2140713e232SGeorge Wilson  * is zfs_condense_pct/100 times the size of the minimal on-disk
2150713e232SGeorge Wilson  * representation, we rewrite it in its minimized form. If a metaslab
2160713e232SGeorge Wilson  * needs to condense then we must set the ms_condensing flag to ensure
2170713e232SGeorge Wilson  * that allocations are not performed on the metaslab that is being written.
218fa9e4066Sahrens  */
219fa9e4066Sahrens struct metaslab {
2200713e232SGeorge Wilson 	kmutex_t	ms_lock;
2210713e232SGeorge Wilson 	kcondvar_t	ms_load_cv;
2220713e232SGeorge Wilson 	space_map_t	*ms_sm;
2230713e232SGeorge Wilson 	metaslab_ops_t	*ms_ops;
2240713e232SGeorge Wilson 	uint64_t	ms_id;
2250713e232SGeorge Wilson 	uint64_t	ms_start;
2260713e232SGeorge Wilson 	uint64_t	ms_size;
2272e4c9986SGeorge Wilson 	uint64_t	ms_fragmentation;
2280713e232SGeorge Wilson 
2290713e232SGeorge Wilson 	range_tree_t	*ms_alloctree[TXG_SIZE];
2300713e232SGeorge Wilson 	range_tree_t	*ms_freetree[TXG_SIZE];
2310713e232SGeorge Wilson 	range_tree_t	*ms_defertree[TXG_DEFER_SIZE];
2320713e232SGeorge Wilson 	range_tree_t	*ms_tree;
2330713e232SGeorge Wilson 
2340713e232SGeorge Wilson 	boolean_t	ms_condensing;	/* condensing? */
2352e4c9986SGeorge Wilson 	boolean_t	ms_condense_wanted;
2360713e232SGeorge Wilson 	boolean_t	ms_loaded;
2370713e232SGeorge Wilson 	boolean_t	ms_loading;
2380713e232SGeorge Wilson 
239468c413aSTim Haley 	int64_t		ms_deferspace;	/* sum of ms_defermap[] space	*/
240ecc2d604Sbonwick 	uint64_t	ms_weight;	/* weight vs. others in group	*/
2410713e232SGeorge Wilson 	uint64_t	ms_access_txg;
2420713e232SGeorge Wilson 
2430713e232SGeorge Wilson 	/*
2440713e232SGeorge Wilson 	 * The metaslab block allocators can optionally use a size-ordered
2450713e232SGeorge Wilson 	 * range tree and/or an array of LBAs. Not all allocators use
2460713e232SGeorge Wilson 	 * this functionality. The ms_size_tree should always contain the
2470713e232SGeorge Wilson 	 * same number of segments as the ms_tree. The only difference
2480713e232SGeorge Wilson 	 * is that the ms_size_tree is ordered by segment sizes.
2490713e232SGeorge Wilson 	 */
2500713e232SGeorge Wilson 	avl_tree_t	ms_size_tree;
2510713e232SGeorge Wilson 	uint64_t	ms_lbas[MAX_LBAS];
2520713e232SGeorge Wilson 
253ecc2d604Sbonwick 	metaslab_group_t *ms_group;	/* metaslab group		*/
254ecc2d604Sbonwick 	avl_node_t	ms_group_node;	/* node in metaslab group tree	*/
255ecc2d604Sbonwick 	txg_node_t	ms_txg_node;	/* per-txg dirty metaslab links	*/
256fa9e4066Sahrens };
257fa9e4066Sahrens 
258fa9e4066Sahrens #ifdef	__cplusplus
259fa9e4066Sahrens }
260fa9e4066Sahrens #endif
261fa9e4066Sahrens 
262fa9e4066Sahrens #endif	/* _SYS_METASLAB_IMPL_H */
263