1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd/*
22209962Smm * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23168404Spjd * Use is subject to license terms.
24168404Spjd */
25168404Spjd
26244087Smm/*
27262094Savg * Copyright (c) 2013 by Delphix. All rights reserved.
28244087Smm */
29244087Smm
30168404Spjd#ifndef _SYS_SPACE_MAP_H
31168404Spjd#define	_SYS_SPACE_MAP_H
32168404Spjd
33168404Spjd#include <sys/avl.h>
34262094Savg#include <sys/range_tree.h>
35168404Spjd#include <sys/dmu.h>
36168404Spjd
37168404Spjd#ifdef	__cplusplus
38168404Spjdextern "C" {
39168404Spjd#endif
40168404Spjd
41262094Savg/*
42262094Savg * The size of the space map object has increased to include a histogram.
43262094Savg * The SPACE_MAP_SIZE_V0 designates the original size and is used to
44262094Savg * maintain backward compatibility.
45262094Savg */
46262094Savg#define	SPACE_MAP_SIZE_V0	(3 * sizeof (uint64_t))
47262094Savg#define	SPACE_MAP_HISTOGRAM_SIZE(sm)			\
48262094Savg	(sizeof ((sm)->sm_phys->smp_histogram) /	\
49262094Savg	sizeof ((sm)->sm_phys->smp_histogram[0]))
50168404Spjd
51262094Savg/*
52262094Savg * The space_map_phys is the on-disk representation of the space map.
53262094Savg * Consumers of space maps should never reference any of the members of this
54262094Savg * structure directly. These members may only be updated in syncing context.
55262094Savg *
56262094Savg * Note the smp_object is no longer used but remains in the structure
57262094Savg * for backward compatibility.
58262094Savg */
59262094Savgtypedef struct space_map_phys {
60262094Savg	uint64_t	smp_object;	/* on-disk space map object */
61262094Savg	uint64_t	smp_objsize;	/* size of the object */
62262094Savg	uint64_t	smp_alloc;	/* space allocated from the map */
63262094Savg	uint64_t	smp_pad[5];	/* reserved */
64262094Savg
65262094Savg	/*
66262094Savg	 * The smp_histogram maintains a histogram of free regions. Each
67262094Savg	 * bucket, smp_histogram[i], contains the number of free regions
68262094Savg	 * whose size is:
69262094Savg	 * 2^(i+sm_shift) <= size of free region in bytes < 2^(i+sm_shift+1)
70262094Savg	 */
71262094Savg	uint64_t	smp_histogram[32]; /* histogram of free space */
72262094Savg} space_map_phys_t;
73262094Savg
74262094Savg/*
75262094Savg * The space map object defines a region of space, its size, how much is
76262094Savg * allocated, and the on-disk object that stores this information.
77262094Savg * Consumers of space maps may only access the members of this structure.
78262094Savg */
79168404Spjdtypedef struct space_map {
80168404Spjd	uint64_t	sm_start;	/* start of map */
81168404Spjd	uint64_t	sm_size;	/* size of map */
82168404Spjd	uint8_t		sm_shift;	/* unit shift */
83262094Savg	uint64_t	sm_length;	/* synced length */
84262094Savg	uint64_t	sm_alloc;	/* synced space allocated */
85262094Savg	objset_t	*sm_os;		/* objset for this map */
86262094Savg	uint64_t	sm_object;	/* object id for this map */
87262094Savg	uint32_t	sm_blksz;	/* block size for space map */
88262094Savg	dmu_buf_t	*sm_dbuf;	/* space_map_phys_t dbuf */
89262094Savg	space_map_phys_t *sm_phys;	/* on-disk space map */
90168404Spjd	kmutex_t	*sm_lock;	/* pointer to lock that protects map */
91168404Spjd} space_map_t;
92168404Spjd
93168404Spjd/*
94168404Spjd * debug entry
95168404Spjd *
96168404Spjd *    1      3         10                     50
97168404Spjd *  ,---+--------+------------+---------------------------------.
98168404Spjd *  | 1 | action |  syncpass  |        txg (lower bits)         |
99168404Spjd *  `---+--------+------------+---------------------------------'
100168404Spjd *   63  62    60 59        50 49                               0
101168404Spjd *
102168404Spjd *
103168404Spjd * non-debug entry
104168404Spjd *
105168404Spjd *    1               47                   1           15
106168404Spjd *  ,-----------------------------------------------------------.
107168404Spjd *  | 0 |   offset (sm_shift units)    | type |       run       |
108168404Spjd *  `-----------------------------------------------------------'
109168404Spjd *   63  62                          17   16   15               0
110168404Spjd */
111168404Spjd
112168404Spjd/* All this stuff takes and returns bytes */
113168404Spjd#define	SM_RUN_DECODE(x)	(BF64_DECODE(x, 0, 15) + 1)
114168404Spjd#define	SM_RUN_ENCODE(x)	BF64_ENCODE((x) - 1, 0, 15)
115168404Spjd#define	SM_TYPE_DECODE(x)	BF64_DECODE(x, 15, 1)
116168404Spjd#define	SM_TYPE_ENCODE(x)	BF64_ENCODE(x, 15, 1)
117168404Spjd#define	SM_OFFSET_DECODE(x)	BF64_DECODE(x, 16, 47)
118168404Spjd#define	SM_OFFSET_ENCODE(x)	BF64_ENCODE(x, 16, 47)
119168404Spjd#define	SM_DEBUG_DECODE(x)	BF64_DECODE(x, 63, 1)
120168404Spjd#define	SM_DEBUG_ENCODE(x)	BF64_ENCODE(x, 63, 1)
121168404Spjd
122168404Spjd#define	SM_DEBUG_ACTION_DECODE(x)	BF64_DECODE(x, 60, 3)
123168404Spjd#define	SM_DEBUG_ACTION_ENCODE(x)	BF64_ENCODE(x, 60, 3)
124168404Spjd
125168404Spjd#define	SM_DEBUG_SYNCPASS_DECODE(x)	BF64_DECODE(x, 50, 10)
126168404Spjd#define	SM_DEBUG_SYNCPASS_ENCODE(x)	BF64_ENCODE(x, 50, 10)
127168404Spjd
128168404Spjd#define	SM_DEBUG_TXG_DECODE(x)		BF64_DECODE(x, 0, 50)
129168404Spjd#define	SM_DEBUG_TXG_ENCODE(x)		BF64_ENCODE(x, 0, 50)
130168404Spjd
131168404Spjd#define	SM_RUN_MAX			SM_RUN_DECODE(~0ULL)
132168404Spjd
133262094Savgtypedef enum {
134262094Savg	SM_ALLOC,
135262094Savg	SM_FREE
136262094Savg} maptype_t;
137168404Spjd
138168404Spjd/*
139168404Spjd * The data for a given space map can be kept on blocks of any size.
140168404Spjd * Larger blocks entail fewer i/o operations, but they also cause the
141168404Spjd * DMU to keep more data in-core, and also to waste more i/o bandwidth
142168404Spjd * when only a few blocks have changed since the last transaction group.
143262094Savg * Rather than having a fixed block size for all space maps the block size
144262094Savg * can adjust as needed (see space_map_max_blksz). Set the initial block
145262094Savg * size for the space map to 4k.
146168404Spjd */
147262094Savg#define	SPACE_MAP_INITIAL_BLOCKSIZE	(1ULL << 12)
148168404Spjd
149262094Savgint space_map_load(space_map_t *sm, range_tree_t *rt, maptype_t maptype);
150168404Spjd
151262094Savgvoid space_map_histogram_clear(space_map_t *sm);
152262094Savgvoid space_map_histogram_add(space_map_t *sm, range_tree_t *rt,
153262094Savg    dmu_tx_t *tx);
154168404Spjd
155262094Savgvoid space_map_update(space_map_t *sm);
156168404Spjd
157262094Savguint64_t space_map_object(space_map_t *sm);
158262094Savguint64_t space_map_allocated(space_map_t *sm);
159262094Savguint64_t space_map_length(space_map_t *sm);
160168404Spjd
161262094Savgvoid space_map_write(space_map_t *sm, range_tree_t *rt, maptype_t maptype,
162262094Savg    dmu_tx_t *tx);
163262094Savgvoid space_map_truncate(space_map_t *sm, dmu_tx_t *tx);
164262094Savguint64_t space_map_alloc(objset_t *os, dmu_tx_t *tx);
165262094Savgvoid space_map_free(space_map_t *sm, dmu_tx_t *tx);
166168404Spjd
167262094Savgint space_map_open(space_map_t **smp, objset_t *os, uint64_t object,
168262094Savg    uint64_t start, uint64_t size, uint8_t shift, kmutex_t *lp);
169262094Savgvoid space_map_close(space_map_t *sm);
170209962Smm
171262094Savgint64_t space_map_alloc_delta(space_map_t *sm);
172262094Savg
173168404Spjd#ifdef	__cplusplus
174168404Spjd}
175168404Spjd#endif
176168404Spjd
177168404Spjd#endif	/* _SYS_SPACE_MAP_H */
178