1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5f65e61cahrens * Common Development and Distribution License (the "License").
6f65e61cahrens * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
211c17160Kevin Crowe
22fa9e406ahrens/*
233f9d6adLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24ae97279Matthew Ahrens * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
25bc9014eJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26c3d26abMatthew Ahrens * Copyright (c) 2014 Integros [integros.com]
271c17160Kevin Crowe * Copyright 2017 Nexenta Systems, Inc.
28fa9e406ahrens */
29fa9e406ahrens
30fa9e406ahrens#ifndef	_SYS_ZAP_IMPL_H
31fa9e406ahrens#define	_SYS_ZAP_IMPL_H
32fa9e406ahrens
33fa9e406ahrens#include <sys/zap.h>
34fa9e406ahrens#include <sys/zfs_context.h>
35fa9e406ahrens#include <sys/avl.h>
36fa9e406ahrens
37fa9e406ahrens#ifdef	__cplusplus
38fa9e406ahrensextern "C" {
39fa9e406ahrens#endif
40fa9e406ahrens
41f65e61cahrensextern int fzap_default_block_shift;
42f65e61cahrens
435ad8204nd#define	ZAP_MAGIC 0x2F52AB2ABULL
44fa9e406ahrens
45f65e61cahrens#define	FZAP_BLOCK_SHIFT(zap)	((zap)->zap_f.zap_block_shift)
46fa9e406ahrens
47fa9e406ahrens#define	MZAP_ENT_LEN		64
48fa9e406ahrens#define	MZAP_NAME_LEN		(MZAP_ENT_LEN - 8 - 4 - 2)
49b515258Matthew Ahrens#define	MZAP_MAX_BLKSZ		SPA_OLD_MAXBLOCKSIZE
50fa9e406ahrens
51b24ab67Jeff Bonwick#define	ZAP_NEED_CD		(-1U)
52b24ab67Jeff Bonwick
53fa9e406ahrenstypedef struct mzap_ent_phys {
54fa9e406ahrens	uint64_t mze_value;
55fa9e406ahrens	uint32_t mze_cd;
56fa9e406ahrens	uint16_t mze_pad;	/* in case we want to chain them someday */
57fa9e406ahrens	char mze_name[MZAP_NAME_LEN];
58fa9e406ahrens} mzap_ent_phys_t;
59fa9e406ahrens
60fa9e406ahrenstypedef struct mzap_phys {
61fa9e406ahrens	uint64_t mz_block_type;	/* ZBT_MICRO */
62fa9e406ahrens	uint64_t mz_salt;
63da6c28aamw	uint64_t mz_normflags;
64da6c28aamw	uint64_t mz_pad[5];
65fa9e406ahrens	mzap_ent_phys_t mz_chunk[1];
66fa9e406ahrens	/* actually variable size depending on block size */
67fa9e406ahrens} mzap_phys_t;
68fa9e406ahrens
69fa9e406ahrenstypedef struct mzap_ent {
70fa9e406ahrens	avl_node_t mze_node;
71fa9e406ahrens	int mze_chunkid;
72fa9e406ahrens	uint64_t mze_hash;
733f9d6adLin Ling	uint32_t mze_cd; /* copy from mze_phys->mze_cd */
74fa9e406ahrens} mzap_ent_t;
75fa9e406ahrens
763f9d6adLin Ling#define	MZE_PHYS(zap, mze) \
77c137962Justin T. Gibbs	(&zap_m_phys(zap)->mz_chunk[(mze)->mze_chunkid])
783f9d6adLin Ling
79fa9e406ahrens/*
80fa9e406ahrens * The (fat) zap is stored in one object. It is an array of
81f65e61cahrens * 1<<FZAP_BLOCK_SHIFT byte blocks. The layout looks like one of:
82fa9e406ahrens *
83fa9e406ahrens * ptrtbl fits in first block:
84fa9e406ahrens * 	[zap_phys_t zap_ptrtbl_shift < 6] [zap_leaf_t] ...
85fa9e406ahrens *
86fa9e406ahrens * ptrtbl too big for first block:
87fa9e406ahrens * 	[zap_phys_t zap_ptrtbl_shift >= 6] [zap_leaf_t] [ptrtbl] ...
88fa9e406ahrens *
89fa9e406ahrens */
90fa9e406ahrens
91fa9e406ahrensstruct dmu_buf;
92fa9e406ahrensstruct zap_leaf;
93fa9e406ahrens
94fa9e406ahrens#define	ZBT_LEAF		((1ULL << 63) + 0)
95fa9e406ahrens#define	ZBT_HEADER		((1ULL << 63) + 1)
96fa9e406ahrens#define	ZBT_MICRO		((1ULL << 63) + 3)
97fa9e406ahrens/* any other values are ptrtbl blocks */
98fa9e406ahrens
99f65e61cahrens/*
100f65e61cahrens * the embedded pointer table takes up half a block:
101f65e61cahrens * block size / entry size (2^3) / 2
102f65e61cahrens */
103f65e61cahrens#define	ZAP_EMBEDDED_PTRTBL_SHIFT(zap) (FZAP_BLOCK_SHIFT(zap) - 3 - 1)
104f65e61cahrens
105f65e61cahrens/*
106f65e61cahrens * The embedded pointer table starts half-way through the block.  Since
107f65e61cahrens * the pointer table itself is half the block, it starts at (64-bit)
108f65e61cahrens * word number (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)).
109f65e61cahrens */
110f65e61cahrens#define	ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) \
111c137962Justin T. Gibbs	((uint64_t *)zap_f_phys(zap)) \
112f65e61cahrens	[(idx) + (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap))]
113fa9e406ahrens
114fa9e406ahrens/*
115fa9e406ahrens * TAKE NOTE:
116fa9e406ahrens * If zap_phys_t is modified, zap_byteswap() must be modified.
117fa9e406ahrens */
118fa9e406ahrenstypedef struct zap_phys {
119fa9e406ahrens	uint64_t zap_block_type;	/* ZBT_HEADER */
120fa9e406ahrens	uint64_t zap_magic;		/* ZAP_MAGIC */
121fa9e406ahrens
122fa9e406ahrens	struct zap_table_phys {
123fa9e406ahrens		uint64_t zt_blk;	/* starting block number */
124fa9e406ahrens		uint64_t zt_numblks;	/* number of blocks */
125fa9e406ahrens		uint64_t zt_shift;	/* bits to index it */
126fa9e406ahrens		uint64_t zt_nextblk;	/* next (larger) copy start block */
127fa9e406ahrens		uint64_t zt_blks_copied; /* number source blocks copied */
128fa9e406ahrens	} zap_ptrtbl;
129fa9e406ahrens
130fa9e406ahrens	uint64_t zap_freeblk;		/* the next free block */
131fa9e406ahrens	uint64_t zap_num_leafs;		/* number of leafs */
132fa9e406ahrens	uint64_t zap_num_entries;	/* number of entries */
133fa9e406ahrens	uint64_t zap_salt;		/* salt to stir into hash function */
134da6c28aamw	uint64_t zap_normflags;		/* flags for u8_textprep_str() */
135b24ab67Jeff Bonwick	uint64_t zap_flags;		/* zap_flags_t */
136f65e61cahrens	/*
137f65e61cahrens	 * This structure is followed by padding, and then the embedded
138f65e61cahrens	 * pointer table.  The embedded pointer table takes up second
139f65e61cahrens	 * half of the block.  It is accessed using the
140f65e61cahrens	 * ZAP_EMBEDDED_PTRTBL_ENT() macro.
141f65e61cahrens	 */
142fa9e406ahrens} zap_phys_t;
143fa9e406ahrens
144fa9e406ahrenstypedef struct zap_table_phys zap_table_phys_t;
145fa9e406ahrens
146fa9e406ahrenstypedef struct zap {
147bc9014eJustin Gibbs	dmu_buf_user_t zap_dbu;
148fa9e406ahrens	objset_t *zap_objset;
149fa9e406ahrens	uint64_t zap_object;
150fa9e406ahrens	struct dmu_buf *zap_dbuf;
151fa9e406ahrens	krwlock_t zap_rwlock;
152da6c28aamw	boolean_t zap_ismicro;
153da6c28aamw	int zap_normflags;
154fa9e406ahrens	uint64_t zap_salt;
155fa9e406ahrens	union {
156fa9e406ahrens		struct {
157fa9e406ahrens			/*
158fa9e406ahrens			 * zap_num_entries_mtx protects
159fa9e406ahrens			 * zap_num_entries
160fa9e406ahrens			 */
161fa9e406ahrens			kmutex_t zap_num_entries_mtx;
162f65e61cahrens			int zap_block_shift;
163fa9e406ahrens		} zap_fat;
164fa9e406ahrens		struct {
165fa9e406ahrens			int16_t zap_num_entries;
166fa9e406ahrens			int16_t zap_num_chunks;
167fa9e406ahrens			int16_t zap_alloc_next;
168fa9e406ahrens			avl_tree_t zap_avl;
169fa9e406ahrens		} zap_micro;
170fa9e406ahrens	} zap_u;
171fa9e406ahrens} zap_t;
172fa9e406ahrens
173c137962Justin T. Gibbsinline zap_phys_t *
174c137962Justin T. Gibbszap_f_phys(zap_t *zap)
175c137962Justin T. Gibbs{
176c137962Justin T. Gibbs	return (zap->zap_dbuf->db_data);
177c137962Justin T. Gibbs}
178c137962Justin T. Gibbs
179c137962Justin T. Gibbsinline mzap_phys_t *
180c137962Justin T. Gibbszap_m_phys(zap_t *zap)
181c137962Justin T. Gibbs{
182c137962Justin T. Gibbs	return (zap->zap_dbuf->db_data);
183c137962Justin T. Gibbs}
184c137962Justin T. Gibbs
185da6c28aamwtypedef struct zap_name {
186da6c28aamw	zap_t *zn_zap;
187b24ab67Jeff Bonwick	int zn_key_intlen;
188b24ab67Jeff Bonwick	const void *zn_key_orig;
189486ae71Matthew Ahrens	int zn_key_orig_numints;
190b24ab67Jeff Bonwick	const void *zn_key_norm;
191486ae71Matthew Ahrens	int zn_key_norm_numints;
192da6c28aamw	uint64_t zn_hash;
193da6c28aamw	matchtype_t zn_matchtype;
1941c17160Kevin Crowe	int zn_normflags;
195da6c28aamw	char zn_normbuf[ZAP_MAXNAMELEN];
196da6c28aamw} zap_name_t;
197da6c28aamw
198fa9e406ahrens#define	zap_f	zap_u.zap_fat
199fa9e406ahrens#define	zap_m	zap_u.zap_micro
200fa9e406ahrens
201da6c28aamwboolean_t zap_match(zap_name_t *zn, const char *matchname);
202fa9e406ahrensint zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
203ae97279Matthew Ahrens    krw_t lti, boolean_t fatreader, boolean_t adding, void *tag, zap_t **zapp);
204ae97279Matthew Ahrensvoid zap_unlockdir(zap_t *zap, void *tag);
20540510e8Josef 'Jeff' Sipekvoid zap_evict_sync(void *dbu);
206b24ab67Jeff Bonwickzap_name_t *zap_name_alloc(zap_t *zap, const char *key, matchtype_t mt);
207da6c28aamwvoid zap_name_free(zap_name_t *zn);
208b24ab67Jeff Bonwickint zap_hashbits(zap_t *zap);
209b24ab67Jeff Bonwickuint32_t zap_maxcd(zap_t *zap);
210b24ab67Jeff Bonwickuint64_t zap_getflags(zap_t *zap);
211fa9e406ahrens
212fa9e406ahrens#define	ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n))))
213fa9e406ahrens
214fa9e406ahrensvoid fzap_byteswap(void *buf, size_t size);
215fa9e406ahrensint fzap_count(zap_t *zap, uint64_t *count);
216da6c28aamwint fzap_lookup(zap_name_t *zn,
217da6c28aamw    uint64_t integer_size, uint64_t num_integers, void *buf,
218da6c28aamw    char *realname, int rn_len, boolean_t *normalization_conflictp);
219c7cd242George Wilsonvoid fzap_prefetch(zap_name_t *zn);
220da6c28aamwint fzap_add(zap_name_t *zn, uint64_t integer_size, uint64_t num_integers,
221ae97279Matthew Ahrens    const void *val, void *tag, dmu_tx_t *tx);
222da6c28aamwint fzap_update(zap_name_t *zn,
223ae97279Matthew Ahrens    int integer_size, uint64_t num_integers, const void *val,
224ae97279Matthew Ahrens    void *tag, dmu_tx_t *tx);
225da6c28aamwint fzap_length(zap_name_t *zn,
226fa9e406ahrens    uint64_t *integer_size, uint64_t *num_integers);
227da6c28aamwint fzap_remove(zap_name_t *zn, dmu_tx_t *tx);
228fa9e406ahrensint fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za);
229fa9e406ahrensvoid fzap_get_stats(zap_t *zap, zap_stats_t *zs);
23087e5029ahrensvoid zap_put_leaf(struct zap_leaf *l);
231fa9e406ahrens
232da6c28aamwint fzap_add_cd(zap_name_t *zn,
233fa9e406ahrens    uint64_t integer_size, uint64_t num_integers,
234ae97279Matthew Ahrens    const void *val, uint32_t cd, void *tag, dmu_tx_t *tx);
235b24ab67Jeff Bonwickvoid fzap_upgrade(zap_t *zap, dmu_tx_t *tx, zap_flags_t flags);
236fa9e406ahrens
237fa9e406ahrens#ifdef	__cplusplus
238fa9e406ahrens}
239fa9e406ahrens#endif
240fa9e406ahrens
241fa9e406ahrens#endif /* _SYS_ZAP_IMPL_H */
242