xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/zap_impl.h (revision 87e5029a3226958edab1512d6182bc74d8d80c9a)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5fa9e4066Sahrens  * Common Development and Distribution License, Version 1.0 only
6fa9e4066Sahrens  * (the "License").  You may not use this file except in compliance
7fa9e4066Sahrens  * with the License.
8fa9e4066Sahrens  *
9fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
11fa9e4066Sahrens  * See the License for the specific language governing permissions
12fa9e4066Sahrens  * and limitations under the License.
13fa9e4066Sahrens  *
14fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
15fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
17fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
18fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
19fa9e4066Sahrens  *
20fa9e4066Sahrens  * CDDL HEADER END
21fa9e4066Sahrens  */
22fa9e4066Sahrens /*
23fa9e4066Sahrens  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24fa9e4066Sahrens  * Use is subject to license terms.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #ifndef	_SYS_ZAP_IMPL_H
28fa9e4066Sahrens #define	_SYS_ZAP_IMPL_H
29fa9e4066Sahrens 
30fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
31fa9e4066Sahrens 
32fa9e4066Sahrens #include <sys/zap.h>
33fa9e4066Sahrens #include <sys/zfs_context.h>
34fa9e4066Sahrens #include <sys/avl.h>
35fa9e4066Sahrens 
36fa9e4066Sahrens #ifdef	__cplusplus
37fa9e4066Sahrens extern "C" {
38fa9e4066Sahrens #endif
39fa9e4066Sahrens 
40fa9e4066Sahrens #define	ZAP_MAGIC 0x2F52AB2AB
41fa9e4066Sahrens 
42fa9e4066Sahrens #define	ZAP_BLOCK_SHIFT		17
43fa9e4066Sahrens 
44fa9e4066Sahrens #define	ZAP_MAXCD		(uint32_t)(-1)
45fa9e4066Sahrens #define	ZAP_HASHBITS		28
46fa9e4066Sahrens #define	MZAP_ENT_LEN		64
47fa9e4066Sahrens #define	MZAP_NAME_LEN		(MZAP_ENT_LEN - 8 - 4 - 2)
48fa9e4066Sahrens #define	MZAP_MAX_BLKSHIFT	ZAP_BLOCK_SHIFT
49fa9e4066Sahrens #define	MZAP_MAX_BLKSZ		(1 << MZAP_MAX_BLKSHIFT)
50fa9e4066Sahrens 
51fa9e4066Sahrens typedef struct mzap_ent_phys {
52fa9e4066Sahrens 	uint64_t mze_value;
53fa9e4066Sahrens 	uint32_t mze_cd;
54fa9e4066Sahrens 	uint16_t mze_pad;	/* in case we want to chain them someday */
55fa9e4066Sahrens 	char mze_name[MZAP_NAME_LEN];
56fa9e4066Sahrens } mzap_ent_phys_t;
57fa9e4066Sahrens 
58fa9e4066Sahrens typedef struct mzap_phys {
59fa9e4066Sahrens 	uint64_t mz_block_type;	/* ZBT_MICRO */
60fa9e4066Sahrens 	uint64_t mz_salt;
61fa9e4066Sahrens 	uint64_t mz_pad[6];
62fa9e4066Sahrens 	mzap_ent_phys_t mz_chunk[1];
63fa9e4066Sahrens 	/* actually variable size depending on block size */
64fa9e4066Sahrens } mzap_phys_t;
65fa9e4066Sahrens 
66fa9e4066Sahrens typedef struct mzap_ent {
67fa9e4066Sahrens 	avl_node_t mze_node;
68fa9e4066Sahrens 	int mze_chunkid;
69fa9e4066Sahrens 	uint64_t mze_hash;
70fa9e4066Sahrens 	mzap_ent_phys_t mze_phys;
71fa9e4066Sahrens } mzap_ent_t;
72fa9e4066Sahrens 
73fa9e4066Sahrens 
74fa9e4066Sahrens /*
75fa9e4066Sahrens  * The (fat) zap is stored in one object. It is an array of
76fa9e4066Sahrens  * 1<<ZAP_BLOCK_SHIFT byte blocks. The layout looks like one of:
77fa9e4066Sahrens  *
78fa9e4066Sahrens  * ptrtbl fits in first block:
79fa9e4066Sahrens  * 	[zap_phys_t zap_ptrtbl_shift < 6] [zap_leaf_t] ...
80fa9e4066Sahrens  *
81fa9e4066Sahrens  * ptrtbl too big for first block:
82fa9e4066Sahrens  * 	[zap_phys_t zap_ptrtbl_shift >= 6] [zap_leaf_t] [ptrtbl] ...
83fa9e4066Sahrens  *
84fa9e4066Sahrens  */
85fa9e4066Sahrens 
86fa9e4066Sahrens struct dmu_buf;
87fa9e4066Sahrens struct zap_leaf;
88fa9e4066Sahrens 
89fa9e4066Sahrens #define	ZBT_LEAF		((1ULL << 63) + 0)
90fa9e4066Sahrens #define	ZBT_HEADER		((1ULL << 63) + 1)
91fa9e4066Sahrens #define	ZBT_MICRO		((1ULL << 63) + 3)
92fa9e4066Sahrens /* any other values are ptrtbl blocks */
93fa9e4066Sahrens 
94fa9e4066Sahrens /* 1/2 the block size */
95fa9e4066Sahrens #define	ZAP_PTRTBL_MIN_SHIFT (ZAP_BLOCK_SHIFT - 3 - 1)
96fa9e4066Sahrens 
97fa9e4066Sahrens /*
98fa9e4066Sahrens  * TAKE NOTE:
99fa9e4066Sahrens  * If zap_phys_t is modified, zap_byteswap() must be modified.
100fa9e4066Sahrens  */
101fa9e4066Sahrens typedef struct zap_phys {
102fa9e4066Sahrens 	uint64_t zap_block_type;	/* ZBT_HEADER */
103fa9e4066Sahrens 	uint64_t zap_magic;		/* ZAP_MAGIC */
104fa9e4066Sahrens 
105fa9e4066Sahrens 	struct zap_table_phys {
106fa9e4066Sahrens 		uint64_t zt_blk;	/* starting block number */
107fa9e4066Sahrens 		uint64_t zt_numblks;	/* number of blocks */
108fa9e4066Sahrens 		uint64_t zt_shift;	/* bits to index it */
109fa9e4066Sahrens 		uint64_t zt_nextblk;	/* next (larger) copy start block */
110fa9e4066Sahrens 		uint64_t zt_blks_copied; /* number source blocks copied */
111fa9e4066Sahrens 	} zap_ptrtbl;
112fa9e4066Sahrens 
113fa9e4066Sahrens 	uint64_t zap_freeblk;		/* the next free block */
114fa9e4066Sahrens 	uint64_t zap_num_leafs;		/* number of leafs */
115fa9e4066Sahrens 	uint64_t zap_num_entries;	/* number of entries */
116fa9e4066Sahrens 	uint64_t zap_salt;		/* salt to stir into hash function */
117fa9e4066Sahrens 	uint64_t zap_pad[8181];
118fa9e4066Sahrens 	uint64_t zap_leafs[1 << ZAP_PTRTBL_MIN_SHIFT];
119fa9e4066Sahrens } zap_phys_t;
120fa9e4066Sahrens 
121fa9e4066Sahrens typedef struct zap_table_phys zap_table_phys_t;
122fa9e4066Sahrens 
123fa9e4066Sahrens typedef struct zap {
124fa9e4066Sahrens 	objset_t *zap_objset;
125fa9e4066Sahrens 	uint64_t zap_object;
126fa9e4066Sahrens 	struct dmu_buf *zap_dbuf;
127fa9e4066Sahrens 	krwlock_t zap_rwlock;
128fa9e4066Sahrens 	int zap_ismicro;
129fa9e4066Sahrens 	uint64_t zap_salt;
130fa9e4066Sahrens 	union {
131fa9e4066Sahrens 		struct {
132fa9e4066Sahrens 			zap_phys_t *zap_phys;
133fa9e4066Sahrens 
134fa9e4066Sahrens 			/*
135fa9e4066Sahrens 			 * zap_num_entries_mtx protects
136fa9e4066Sahrens 			 * zap_num_entries
137fa9e4066Sahrens 			 */
138fa9e4066Sahrens 			kmutex_t zap_num_entries_mtx;
139fa9e4066Sahrens 		} zap_fat;
140fa9e4066Sahrens 		struct {
141fa9e4066Sahrens 			mzap_phys_t *zap_phys;
142fa9e4066Sahrens 			int16_t zap_num_entries;
143fa9e4066Sahrens 			int16_t zap_num_chunks;
144fa9e4066Sahrens 			int16_t zap_alloc_next;
145fa9e4066Sahrens 			avl_tree_t zap_avl;
146fa9e4066Sahrens 		} zap_micro;
147fa9e4066Sahrens 	} zap_u;
148fa9e4066Sahrens } zap_t;
149fa9e4066Sahrens 
150fa9e4066Sahrens #define	zap_f	zap_u.zap_fat
151fa9e4066Sahrens #define	zap_m	zap_u.zap_micro
152fa9e4066Sahrens 
153fa9e4066Sahrens uint64_t zap_hash(zap_t *zap, const char *name);
154fa9e4066Sahrens int zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
155fa9e4066Sahrens     krw_t lti, int fatreader, zap_t **zapp);
156fa9e4066Sahrens void zap_unlockdir(zap_t *zap);
157fa9e4066Sahrens void zap_pageout(dmu_buf_t *db, void *vmzap);
158fa9e4066Sahrens 
159fa9e4066Sahrens void zap_print(zap_t *);
160fa9e4066Sahrens struct zap_leaf *zap_create_leaf(zap_t *zd, dmu_tx_t *tx);
161fa9e4066Sahrens void zap_destroy_leaf(zap_t *zap, struct zap_leaf *l, dmu_tx_t *tx);
162fa9e4066Sahrens uint64_t zap_allocate_blocks(zap_t *zap, int nblocks, dmu_tx_t *tx);
163fa9e4066Sahrens 
164fa9e4066Sahrens #define	ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n))))
165fa9e4066Sahrens 
166fa9e4066Sahrens void fzap_byteswap(void *buf, size_t size);
167fa9e4066Sahrens int fzap_count(zap_t *zap, uint64_t *count);
168fa9e4066Sahrens int fzap_lookup(zap_t *zap, const char *name,
169fa9e4066Sahrens     uint64_t integer_size, uint64_t num_integers, void *buf);
170fa9e4066Sahrens int fzap_add(zap_t *zap, const char *name,
171fa9e4066Sahrens     uint64_t integer_size, uint64_t num_integers,
172fa9e4066Sahrens     const void *val, dmu_tx_t *tx);
173fa9e4066Sahrens int fzap_update(zap_t *zap, const char *name,
174fa9e4066Sahrens     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx);
175fa9e4066Sahrens int fzap_length(zap_t *zap, const char *name,
176fa9e4066Sahrens     uint64_t *integer_size, uint64_t *num_integers);
177fa9e4066Sahrens int fzap_remove(zap_t *zap, const char *name, dmu_tx_t *tx);
178fa9e4066Sahrens int fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za);
179fa9e4066Sahrens void fzap_get_stats(zap_t *zap, zap_stats_t *zs);
180*87e5029aSahrens void zap_put_leaf(struct zap_leaf *l);
181fa9e4066Sahrens 
182fa9e4066Sahrens int fzap_add_cd(zap_t *zap, const char *name,
183fa9e4066Sahrens     uint64_t integer_size, uint64_t num_integers,
184fa9e4066Sahrens     const void *val, uint32_t cd, dmu_tx_t *tx, struct zap_leaf **lp);
185fa9e4066Sahrens void fzap_upgrade(zap_t *zap, dmu_tx_t *tx);
186fa9e4066Sahrens 
187fa9e4066Sahrens #ifdef	__cplusplus
188fa9e4066Sahrens }
189fa9e4066Sahrens #endif
190fa9e4066Sahrens 
191fa9e4066Sahrens #endif /* _SYS_ZAP_IMPL_H */
192