xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/space_map.h (revision 80ab886d233f514d54c2a6bdeb9fdfd951bd6881)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _SYS_SPACE_MAP_H
28 #define	_SYS_SPACE_MAP_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/avl.h>
33 #include <sys/dmu.h>
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 typedef struct space_map {
40 	avl_tree_t	sm_root;	/* Root of the AVL tree */
41 	uint64_t	sm_start;	/* Start of map (inclusive) */
42 	uint64_t	sm_end;		/* End of map (exclusive) */
43 	uint64_t	sm_size;	/* Size of map (end - start) */
44 	uint64_t	sm_shift;	/* Unit shift */
45 	uint64_t	sm_space;	/* Sum of all segments in the map */
46 	kmutex_t	*sm_lock;	/* pointer to lock that protects map */
47 } space_map_t;
48 
49 typedef struct space_seg {
50 	avl_node_t	ss_node;	/* AVL node */
51 	uint64_t	ss_start;	/* starting offset of this segment */
52 	uint64_t	ss_end;		/* ending offset (non-inclusive) */
53 } space_seg_t;
54 
55 typedef struct space_map_obj {
56 	uint64_t	smo_object;	/* on-disk space map object */
57 	uint64_t	smo_objsize;	/* size of the object */
58 	uint64_t	smo_alloc;	/* space allocated from the map */
59 } space_map_obj_t;
60 
61 /*
62  * debug entry
63  *
64  *    1      3         10                     50
65  *  ,---+--------+------------+---------------------------------.
66  *  | 1 | action |  syncpass  |        txg (lower bits)         |
67  *  `---+--------+------------+---------------------------------'
68  *   63  62    60 59        50 49                               0
69  *
70  *
71  *
72  * non-debug entry
73  *
74  *    1               47                   1           15
75  *  ,-----------------------------------------------------------.
76  *  | 0 |   offset (sm_shift units)    | type |       run       |
77  *  `-----------------------------------------------------------'
78  *   63  62                          17   16   15               0
79  */
80 
81 /* All this stuff takes and returns bytes */
82 #define	SM_RUN_DECODE(x)	(BF64_DECODE(x, 0, 15) + 1)
83 #define	SM_RUN_ENCODE(x)	BF64_ENCODE((x) - 1, 0, 15)
84 #define	SM_TYPE_DECODE(x)	BF64_DECODE(x, 15, 1)
85 #define	SM_TYPE_ENCODE(x)	BF64_ENCODE(x, 15, 1)
86 #define	SM_OFFSET_DECODE(x)	BF64_DECODE(x, 16, 47)
87 #define	SM_OFFSET_ENCODE(x)	BF64_ENCODE(x, 16, 47)
88 #define	SM_DEBUG_DECODE(x)	BF64_DECODE(x, 63, 1)
89 #define	SM_DEBUG_ENCODE(x)	BF64_ENCODE(x, 63, 1)
90 
91 #define	SM_DEBUG_ACTION_DECODE(x)	BF64_DECODE(x, 60, 3)
92 #define	SM_DEBUG_ACTION_ENCODE(x)	BF64_ENCODE(x, 60, 3)
93 
94 #define	SM_DEBUG_SYNCPASS_DECODE(x)	BF64_DECODE(x, 50, 10)
95 #define	SM_DEBUG_SYNCPASS_ENCODE(x)	BF64_ENCODE(x, 50, 10)
96 
97 #define	SM_DEBUG_TXG_DECODE(x)		BF64_DECODE(x, 0, 50)
98 #define	SM_DEBUG_TXG_ENCODE(x)		BF64_ENCODE(x, 0, 50)
99 
100 #define	SM_RUN_MAX			SM_RUN_DECODE(~0ULL)
101 
102 #define	SM_ALLOC	0x0
103 #define	SM_FREE		0x1
104 
105 /*
106  * The data for a given space map can be kept on blocks of any size.
107  * Larger blocks entail fewer i/o operations, but they also cause the
108  * DMU to keep more data in-core, and also to waste more i/o bandwidth
109  * when only a few blocks have changed since the last transaction group.
110  * This could use a lot more research, but for now, set the freelist
111  * block size to 4k (2^12).
112  */
113 #define	SPACE_MAP_BLOCKSHIFT	12
114 
115 #define	SPACE_MAP_CHUNKSIZE	(1<<20)
116 
117 typedef void space_map_func_t(space_map_t *sm, uint64_t start, uint64_t size);
118 
119 extern void space_map_create(space_map_t *sm, uint64_t start, uint64_t size,
120     uint64_t shift, kmutex_t *lp);
121 extern void space_map_destroy(space_map_t *sm);
122 extern void space_map_add(space_map_t *sm, uint64_t start, uint64_t size);
123 extern void space_map_remove(space_map_t *sm, uint64_t start, uint64_t size);
124 extern int space_map_contains(space_map_t *sm, uint64_t start, uint64_t size);
125 extern void space_map_vacate(space_map_t *sm,
126     space_map_func_t *func, space_map_t *mdest);
127 extern void space_map_iterate(space_map_t *sm,
128     space_map_func_t *func, space_map_t *mdest);
129 extern void space_map_merge(space_map_t *dest, space_map_t *src);
130 extern void space_map_excise(space_map_t *sm, uint64_t start, uint64_t size);
131 extern void space_map_union(space_map_t *smd, space_map_t *sms);
132 
133 extern int space_map_load(space_map_t *sm, space_map_obj_t *smo,
134     uint8_t maptype, objset_t *os, uint64_t end, uint64_t space);
135 extern void space_map_sync(space_map_t *sm, space_map_t *dest,
136     space_map_obj_t *smo, uint8_t maptype, objset_t *os, dmu_tx_t *tx);
137 extern void space_map_write(space_map_t *sm, space_map_obj_t *smo,
138     objset_t *os, dmu_tx_t *tx);
139 
140 #ifdef	__cplusplus
141 }
142 #endif
143 
144 #endif	/* _SYS_SPACE_MAP_H */
145