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/*
22219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23168404Spjd */
24168404Spjd
25168404Spjd#ifndef	_SYS_ZAP_IMPL_H
26168404Spjd#define	_SYS_ZAP_IMPL_H
27168404Spjd
28168404Spjd#include <sys/zap.h>
29168404Spjd#include <sys/zfs_context.h>
30168404Spjd#include <sys/avl.h>
31168404Spjd
32168404Spjd#ifdef	__cplusplus
33168404Spjdextern "C" {
34168404Spjd#endif
35168404Spjd
36168404Spjdextern int fzap_default_block_shift;
37168404Spjd
38168404Spjd#define	ZAP_MAGIC 0x2F52AB2ABULL
39168404Spjd
40168404Spjd#define	FZAP_BLOCK_SHIFT(zap)	((zap)->zap_f.zap_block_shift)
41168404Spjd
42168404Spjd#define	MZAP_ENT_LEN		64
43168404Spjd#define	MZAP_NAME_LEN		(MZAP_ENT_LEN - 8 - 4 - 2)
44168404Spjd#define	MZAP_MAX_BLKSHIFT	SPA_MAXBLOCKSHIFT
45168404Spjd#define	MZAP_MAX_BLKSZ		(1 << MZAP_MAX_BLKSHIFT)
46168404Spjd
47219089Spjd#define	ZAP_NEED_CD		(-1U)
48219089Spjd
49168404Spjdtypedef struct mzap_ent_phys {
50168404Spjd	uint64_t mze_value;
51168404Spjd	uint32_t mze_cd;
52168404Spjd	uint16_t mze_pad;	/* in case we want to chain them someday */
53168404Spjd	char mze_name[MZAP_NAME_LEN];
54168404Spjd} mzap_ent_phys_t;
55168404Spjd
56168404Spjdtypedef struct mzap_phys {
57168404Spjd	uint64_t mz_block_type;	/* ZBT_MICRO */
58168404Spjd	uint64_t mz_salt;
59185029Spjd	uint64_t mz_normflags;
60185029Spjd	uint64_t mz_pad[5];
61168404Spjd	mzap_ent_phys_t mz_chunk[1];
62168404Spjd	/* actually variable size depending on block size */
63168404Spjd} mzap_phys_t;
64168404Spjd
65168404Spjdtypedef struct mzap_ent {
66168404Spjd	avl_node_t mze_node;
67168404Spjd	int mze_chunkid;
68168404Spjd	uint64_t mze_hash;
69219089Spjd	uint32_t mze_cd; /* copy from mze_phys->mze_cd */
70168404Spjd} mzap_ent_t;
71168404Spjd
72219089Spjd#define	MZE_PHYS(zap, mze) \
73219089Spjd	(&(zap)->zap_m.zap_phys->mz_chunk[(mze)->mze_chunkid])
74168404Spjd
75168404Spjd/*
76168404Spjd * The (fat) zap is stored in one object. It is an array of
77168404Spjd * 1<<FZAP_BLOCK_SHIFT byte blocks. The layout looks like one of:
78168404Spjd *
79168404Spjd * ptrtbl fits in first block:
80168404Spjd * 	[zap_phys_t zap_ptrtbl_shift < 6] [zap_leaf_t] ...
81168404Spjd *
82168404Spjd * ptrtbl too big for first block:
83168404Spjd * 	[zap_phys_t zap_ptrtbl_shift >= 6] [zap_leaf_t] [ptrtbl] ...
84168404Spjd *
85168404Spjd */
86168404Spjd
87168404Spjdstruct dmu_buf;
88168404Spjdstruct zap_leaf;
89168404Spjd
90168404Spjd#define	ZBT_LEAF		((1ULL << 63) + 0)
91168404Spjd#define	ZBT_HEADER		((1ULL << 63) + 1)
92168404Spjd#define	ZBT_MICRO		((1ULL << 63) + 3)
93168404Spjd/* any other values are ptrtbl blocks */
94168404Spjd
95168404Spjd/*
96168404Spjd * the embedded pointer table takes up half a block:
97168404Spjd * block size / entry size (2^3) / 2
98168404Spjd */
99168404Spjd#define	ZAP_EMBEDDED_PTRTBL_SHIFT(zap) (FZAP_BLOCK_SHIFT(zap) - 3 - 1)
100168404Spjd
101168404Spjd/*
102168404Spjd * The embedded pointer table starts half-way through the block.  Since
103168404Spjd * the pointer table itself is half the block, it starts at (64-bit)
104168404Spjd * word number (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)).
105168404Spjd */
106168404Spjd#define	ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) \
107168404Spjd	((uint64_t *)(zap)->zap_f.zap_phys) \
108168404Spjd	[(idx) + (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap))]
109168404Spjd
110168404Spjd/*
111168404Spjd * TAKE NOTE:
112168404Spjd * If zap_phys_t is modified, zap_byteswap() must be modified.
113168404Spjd */
114168404Spjdtypedef struct zap_phys {
115168404Spjd	uint64_t zap_block_type;	/* ZBT_HEADER */
116168404Spjd	uint64_t zap_magic;		/* ZAP_MAGIC */
117168404Spjd
118168404Spjd	struct zap_table_phys {
119168404Spjd		uint64_t zt_blk;	/* starting block number */
120168404Spjd		uint64_t zt_numblks;	/* number of blocks */
121168404Spjd		uint64_t zt_shift;	/* bits to index it */
122168404Spjd		uint64_t zt_nextblk;	/* next (larger) copy start block */
123168404Spjd		uint64_t zt_blks_copied; /* number source blocks copied */
124168404Spjd	} zap_ptrtbl;
125168404Spjd
126168404Spjd	uint64_t zap_freeblk;		/* the next free block */
127168404Spjd	uint64_t zap_num_leafs;		/* number of leafs */
128168404Spjd	uint64_t zap_num_entries;	/* number of entries */
129168404Spjd	uint64_t zap_salt;		/* salt to stir into hash function */
130185029Spjd	uint64_t zap_normflags;		/* flags for u8_textprep_str() */
131219089Spjd	uint64_t zap_flags;		/* zap_flags_t */
132168404Spjd	/*
133168404Spjd	 * This structure is followed by padding, and then the embedded
134168404Spjd	 * pointer table.  The embedded pointer table takes up second
135168404Spjd	 * half of the block.  It is accessed using the
136168404Spjd	 * ZAP_EMBEDDED_PTRTBL_ENT() macro.
137168404Spjd	 */
138168404Spjd} zap_phys_t;
139168404Spjd
140168404Spjdtypedef struct zap_table_phys zap_table_phys_t;
141168404Spjd
142168404Spjdtypedef struct zap {
143168404Spjd	objset_t *zap_objset;
144168404Spjd	uint64_t zap_object;
145168404Spjd	struct dmu_buf *zap_dbuf;
146168404Spjd	krwlock_t zap_rwlock;
147185029Spjd	boolean_t zap_ismicro;
148185029Spjd	int zap_normflags;
149168404Spjd	uint64_t zap_salt;
150168404Spjd	union {
151168404Spjd		struct {
152168404Spjd			zap_phys_t *zap_phys;
153168404Spjd
154168404Spjd			/*
155168404Spjd			 * zap_num_entries_mtx protects
156168404Spjd			 * zap_num_entries
157168404Spjd			 */
158168404Spjd			kmutex_t zap_num_entries_mtx;
159168404Spjd			int zap_block_shift;
160168404Spjd		} zap_fat;
161168404Spjd		struct {
162168404Spjd			mzap_phys_t *zap_phys;
163168404Spjd			int16_t zap_num_entries;
164168404Spjd			int16_t zap_num_chunks;
165168404Spjd			int16_t zap_alloc_next;
166168404Spjd			avl_tree_t zap_avl;
167168404Spjd		} zap_micro;
168168404Spjd	} zap_u;
169168404Spjd} zap_t;
170168404Spjd
171185029Spjdtypedef struct zap_name {
172185029Spjd	zap_t *zn_zap;
173219089Spjd	int zn_key_intlen;
174219089Spjd	const void *zn_key_orig;
175219089Spjd	int zn_key_orig_numints;
176219089Spjd	const void *zn_key_norm;
177219089Spjd	int zn_key_norm_numints;
178185029Spjd	uint64_t zn_hash;
179185029Spjd	matchtype_t zn_matchtype;
180185029Spjd	char zn_normbuf[ZAP_MAXNAMELEN];
181185029Spjd} zap_name_t;
182185029Spjd
183168404Spjd#define	zap_f	zap_u.zap_fat
184168404Spjd#define	zap_m	zap_u.zap_micro
185168404Spjd
186185029Spjdboolean_t zap_match(zap_name_t *zn, const char *matchname);
187168404Spjdint zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
188185029Spjd    krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp);
189168404Spjdvoid zap_unlockdir(zap_t *zap);
190168404Spjdvoid zap_evict(dmu_buf_t *db, void *vmzap);
191219089Spjdzap_name_t *zap_name_alloc(zap_t *zap, const char *key, matchtype_t mt);
192185029Spjdvoid zap_name_free(zap_name_t *zn);
193219089Spjdint zap_hashbits(zap_t *zap);
194219089Spjduint32_t zap_maxcd(zap_t *zap);
195219089Spjduint64_t zap_getflags(zap_t *zap);
196168404Spjd
197168404Spjd#define	ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n))))
198168404Spjd
199168404Spjdvoid fzap_byteswap(void *buf, size_t size);
200168404Spjdint fzap_count(zap_t *zap, uint64_t *count);
201185029Spjdint fzap_lookup(zap_name_t *zn,
202185029Spjd    uint64_t integer_size, uint64_t num_integers, void *buf,
203185029Spjd    char *realname, int rn_len, boolean_t *normalization_conflictp);
204219089Spjdvoid fzap_prefetch(zap_name_t *zn);
205209962Smmint fzap_count_write(zap_name_t *zn, int add, uint64_t *towrite,
206209962Smm    uint64_t *tooverwrite);
207185029Spjdint fzap_add(zap_name_t *zn, uint64_t integer_size, uint64_t num_integers,
208168404Spjd    const void *val, dmu_tx_t *tx);
209185029Spjdint fzap_update(zap_name_t *zn,
210168404Spjd    int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx);
211185029Spjdint fzap_length(zap_name_t *zn,
212168404Spjd    uint64_t *integer_size, uint64_t *num_integers);
213185029Spjdint fzap_remove(zap_name_t *zn, dmu_tx_t *tx);
214168404Spjdint fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za);
215168404Spjdvoid fzap_get_stats(zap_t *zap, zap_stats_t *zs);
216168404Spjdvoid zap_put_leaf(struct zap_leaf *l);
217168404Spjd
218185029Spjdint fzap_add_cd(zap_name_t *zn,
219168404Spjd    uint64_t integer_size, uint64_t num_integers,
220168404Spjd    const void *val, uint32_t cd, dmu_tx_t *tx);
221219089Spjdvoid fzap_upgrade(zap_t *zap, dmu_tx_t *tx, zap_flags_t flags);
222219089Spjdint fzap_cursor_move_to_key(zap_cursor_t *zc, zap_name_t *zn);
223168404Spjd
224168404Spjd#ifdef	__cplusplus
225168404Spjd}
226168404Spjd#endif
227168404Spjd
228168404Spjd#endif /* _SYS_ZAP_IMPL_H */
229